Jump to content

Preg_Match_All Loop


-Karl-

Recommended Posts

Hello,

 

I'm trying to find a way to filter out image hosts which aren't currently on a "whitelist", so far I have the following which works fine, but only for one preg_match.

 

So obviously I need to change it to preg_match_all, then to another foreach.

 

Basically, foreach image found, loop through the whitelist. However, I've been experimenting and I just can't get my head around it.

 

This is what I have so far for just checking one instance.

 

 

$txt = '[img=http://i.imgur.com/thisworks.png][img=http://i.imdddgur.com/doesntwork.png]';
	 if(preg_match("#\[img\](.+?)\[/img\]#i",$txt)) {
			 $whitelist = array('tinypic.com','imageshack.us','photobucket.com','imgur.com','deviantart.com','puu.sh','google.com','facebook.com');
			 $good_url = 0;
			 foreach( $whitelist as $url ) {
				 if($good_url == 0) {
					 $url = preg_quote( $url, '/' );
					 $url = str_replace( '\*', "(.*?)", $url );

					 if( preg_match( '/' . $url . '/i', $txt ) ) {
						 $good_url = 1;
					 }
				 }
			 }
			 if(!$good_url) {
				 $this->error = 'invalid_image_host';
			 }
		 }

 

And this is what I had for the preg_match_all which I can't get to work:

$txt = '[img=http://i.imgur.com/thisworks.png][img=http://i.imdddgur.com/doesntwork.png]';
if(preg_match_all("#\[img\](.+?)\[/img\]#i",$txt,$match)) {
			 $whitelist = array('tinypic.com','imageshack.us','photobucket.com','imgur.com','deviantart.com','puu.sh','google.com','facebook.com');
			 $good_url = 0;
			 $i = 0;
			 foreach($match as $image) {
				 foreach( $whitelist as $url ) {
					 if($good_url == 0) {
						 $url = preg_quote( $url, '/' );
						 $url = str_replace( '\*', "(.*?)", $url );

						 if( preg_match( '/' . $url . '/i', $image[$i] ) ) {
							 $good_url = 1;
							 $hi = $image[$i];
						 }
					 }
					 $i++;
				 }
			 }
			 if(!$good_url == 1) {
				 $this->error = 'invalid_image_host';
			 }
		 }

 

Any ideas?

Edited by -Karl-
Link to comment
Share on other sites

This still returns an error, I just can't figure out why...

$txt = '[img=tinypic.com][img=imageshack.us]';
if(preg_match_all("#\[img\](.+?)\[/img\]#i",$txt,$match)) {
			    $whitelist = array('tinypic.com','imageshack.us','photobucket.com','i.imgur.com','imgur.com','deviantart.com','puu.sh','google.com','facebook.com');   
			    $good_url = 0;
			    $i = 0;
			    foreach($match as $image) {
				    foreach( $whitelist as $url ) {
					    $url = preg_quote( $url, '/' );
					    $url = str_replace( '\*', "(.*?)", $url );
					    if( !preg_match( '/' . $url . '/i', $image[$i] ) ) {
						    $good_url = 1;
						    if($good_url = 1) {
							    $bad = $image[$i].$i;
						    }
					    }
				    }
				    $i++;
			    }
			    if($good_url == 1) {
				    echo $bad;
				    //$this->error = 'invalid_image_host';
			    }
		    }

Link to comment
Share on other sites

preg_match() doesn't return what you think it does. Here is what $match contains for you - I did a print_r($match);

 

Array ( [0] => Array ( [0] => [img=tinypic.com] [1] => [img=imageshack.us] ) [1] => Array ( [0] => tinypic.com [1] => imageshack.us ) )

 

So your loop should use $match[1] rather than $match alone.

Link to comment
Share on other sites

Indeed, but I already tried that so was just messing around.

$txt = '[img=tinypic.com][img=imageshack.us]';
if(preg_match_all("#\[img\](.+?)\[/img\]#i",$txt,$match)) {
			    $whitelist = array('tinypic.com','imageshack.us','photobucket.com','pure-warfare.com','i.imgur.com','imgur.com','deviantart.com','runescape.com','jagex.com','puu.sh','google.com','facebook.com');   
			    $good_url = 0;
			    $i = 0;
			    foreach($match[0] as $image) {
				    foreach( $whitelist as $url ) {
					    $url = preg_quote( $url, '/' );
					    $url = str_replace( '\*', "(.*?)", $url );
					    if( preg_match( '/' . $url . '/i', $image[$i] ) ) {
						    $good_url = 1; 
					    }
				    }
				    $i++;
			    }
			    if(!$good_url) {
				    echo 'error';
				    //$this->error = 'invalid_image_host';
			    } else {
				    echo $txt;
			    }
		    }

Still doesn't work, just echos error.

Link to comment
Share on other sites

I see that you have your if(!$good_url) after the foreach, however the string can contain more than one URL. I'm guessing there should be an error per url, rather than for the whole string however I'll leave that up to you.

 

You have $image[$i], this will almost indefinitely return [ at all times. $image contains a string, for instance it may contain facebook.com. I previously told you to use $match[1] but you did not. So not only is there that, you should be using $image on it's own. I also believe your preg_quote is adding unnecessary slashes.

Link to comment
Share on other sites

Guest
This topic is now 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.