Jump to content

Filtering for a phrase using pregmatch


greenheart

Recommended Posts

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

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).

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?

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.

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.