RegEx help needed (remove certain images)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tylerdurden
Forum Commoner
Posts: 66
Joined: Mon Jul 28, 2003 11:52 am
Location: Austria

RegEx help needed (remove certain images)

Post by tylerdurden »

Hi all!

I've arrived at a dead and and could need some help. RegExs are giving me headaches again.
I need to cleanup some text to only allow images from a certain server.
Let's say the text looks like this:

Code: Select all

Some html code
<img src="http://www.allowedserver.com/pics/image.jpg" border="0">
<img src="http://www.deniedserver.com/pics/image.jpg" border="0">
more html code
I need a regex that will remove the whole image tag that contains the image from the http://www.deniedserver.com but leaves the other image intact.

Any help appreciated! Thanks!

tylerd
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

preg_replace('#&lt;\s*img\s+&#1111;^&gt;]*src\s*=\s*(&#1111;"'']?)(http://)?(&#1111;a-z-]+\.)*(?!allowedserver.com)(/&#1111;^/]+)*\\\\1&#1111;^&gt;]*?&gt;#is', '', $text);
Post Reply