itdmacar Posted February 9, 2010 Share Posted February 9, 2010 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 More sharing options...
yozyk Posted February 10, 2010 Share Posted February 10, 2010 Try this reg exp preg_match('# ([a-z]*) Date Entered#i', $string1, $matches); echo $matches[1]; // Dog Link to comment https://forums.phpfreaks.com/topic/191544-string-search-help/#findComment-1010210 Share on other sites More sharing options...
kathas Posted February 10, 2010 Share Posted February 10, 2010 // 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 More sharing options...
itdmacar Posted February 11, 2010 Author Share Posted February 11, 2010 Thank you very much, I will try them all . . . Link to comment https://forums.phpfreaks.com/topic/191544-string-search-help/#findComment-1010959 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.