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. Quote Link to comment 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 Quote Link to comment 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 )); } } Quote Link to comment 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 . . . Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.