greenheart Posted October 18, 2009 Share Posted October 18, 2009 Here is my code which looks for addresses which start with ' src=" ' preg_match_all('#src="(http://[^"]+)#', $value, $matches) I want to modify this so that it only returns matches which contain the phrase "pass". So src="http://www.example.com/blah/pass/edu" would be picked up. Can someone kindly tell me how to do this? Thank you so much. Link to comment https://forums.phpfreaks.com/topic/178155-filtering-for-a-phrase-using-pregmatch/ Share on other sites More sharing options...
mrMarcus Posted October 18, 2009 Share Posted October 18, 2009 have you looked into the preg_match() function at all? i'm assuming you didn't write the code you provided otherwise you'd know how this is done. are you going to need to match it several times in a string, or just stop once (if) it's matched. if the latter of the two, just use preg_match() .. that site is your new PHP bible. go to that link, and try out the examples .. come back with your attempts, and i'll help clean anything up (if necessary). Link to comment https://forums.phpfreaks.com/topic/178155-filtering-for-a-phrase-using-pregmatch/#findComment-939368 Share on other sites More sharing options...
greenheart Posted October 18, 2009 Author Share Posted October 18, 2009 Hello, I have tried with this and variants but no success preg_match_all('#src="(http://[^"]+)#', $value, $matches) $i=0; for ($i = 0; $i < 10; $i++){ preg_match_all('#pass#',$matches[i], $result) print_r($result) } The idea is stop once I have a match. Could you help me please? Link to comment https://forums.phpfreaks.com/topic/178155-filtering-for-a-phrase-using-pregmatch/#findComment-939376 Share on other sites More sharing options...
thebadbad Posted October 18, 2009 Share Posted October 18, 2009 Something like <?php preg_match_all('~\bsrc\s?=\s?([\'"])(http://.+?)\1~is', $value, $matches); foreach ($matches[2] as $url) { if (strpos($url, 'pass') !== false) { echo "Match found: $url"; break; } } ?> You can use stripos() if the search should be case insensitive. Link to comment https://forums.phpfreaks.com/topic/178155-filtering-for-a-phrase-using-pregmatch/#findComment-939377 Share on other sites More sharing options...
mrMarcus Posted October 18, 2009 Share Posted October 18, 2009 use preg_match and not preg_match_all, since you do not need to continue searching after a match has been made/found. Link to comment https://forums.phpfreaks.com/topic/178155-filtering-for-a-phrase-using-pregmatch/#findComment-939379 Share on other sites More sharing options...
greenheart Posted October 18, 2009 Author Share Posted October 18, 2009 Thanks everyone I figured it out. I used the strpos that thebadbad gives in his code and put it in my code and it works. Link to comment https://forums.phpfreaks.com/topic/178155-filtering-for-a-phrase-using-pregmatch/#findComment-939383 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.