1

i have searched through the Internet and found a solution to this but it do it in a reverse order.

original string

S0597 KIPATIMU SECONDARY DIV-I = 0  DIV-II = 3  

string to be extracted

S0597 KIPATIMU SECONDARY

extraction should stop when "DIV-" is reached.

my code output

DIV-I = 0  DIV-II = 3

here is my code.

if(($pos = strpos($string, 'DIV-')) !== false)
{
  $school_name = substr($string, $pos);
}
else
{
   $school_name = get_last_word($string);
}
echo $school_name;

how can i achieve this?

3 Answers 3

4

$str_value='S0597 KIPATIMU SECONDARY DIV-I = 0 DIV-II = 3' //store your string value

In PHP there is many function availble for these you have used the more than one function to achive these.

stristr($str_value,"DIV-",true); //these function return the o/p : S0597 KIPATIMU SECONDARY

If you change the last argument from 'true' to 'false' it will return the DIV-I = 0 DIV-II = 3

Sign up to request clarification or add additional context in comments.

Comments

1
$school_name = substr($string, $pos);

It means you are assigning $school_name with substring of $string, start from where the DIV- reached to the end of $string.

You need to start it from 0 until the position where DIV- is reached.

$school_name = substr($string, 0, $pos);

Comments

1

you get extraction string in $school_name then replace this with blank from full string and you will get output try

if(($pos = strpos($string, 'DIV-')) !== false)
{
  $school_name = substr($string, $pos);
  $school_name = str_replace($school_name, '', $string);
}
// output - S0597 KIPATIMU SECONDARY 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.