Jump to content

String search help


itdmacar

Recommended Posts

Hi,

 

Can somebody please help me on my string  problem . . .

 

I want to retrieve a particular text in a string, see example below I want to retreive the animal type on each strings

 

$string1 = 'blah blah blah blah blah Animal Dog Date Entered blah blah blah';

 

$string2 = 'blah Hippopotamus Date Entered blah blah blah blah' ;

 

$string3 = 'blah blah blah blah blah blah blah Animal Birds Date Entered';

 

 

$string1 output will be Dog

$string2 output will beHippopotamus

$string3 output will be Birds

 

Basically I want to extract the string between 'Animal' and 'Date Entered'.

 

Can somebody please help me on this . . .

 

Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/191544-string-search-help/
Share on other sites

// one way
$string = trim(substr($string, strpos($string,"Dog")+strlen("Dog") , strpos($string,"Date Entered") - strpos($string,"Dog")+strlen("Dog") ));

// better way

$x = strpos($string,"Dog");
if ($x!==FALSE)
{
     $y = strpos($string,"Date Entered");
     if ($y!==FALSE && $y>$x)
     {
          $x += strlen("Dog");
          $string =  trim(substr($string, $x , $y-$x ));
     }
}

Link to comment
https://forums.phpfreaks.com/topic/191544-string-search-help/#findComment-1010214
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.