...

View Full Version : preg_replace to remove links containing custom word



roblogger
05-03-2009, 01:27 AM
I want to remove completely the links and images containing doubleclick and pheedo with preg_replace


<a href="http://www.donotremove.com/"><img src="http://donotremove.net/~a/DoZC.jpg"/></a>
<img src="http://donotremove.net/22222.jpg"/>
<a href="http://feedads.g.doubleclick.net/~a/DoZCMoSsD67yAdsBepwwwwfo5M/0/da">
<img src="http://feedads.g.doubleclick.net/~a/DoZC.jpg"/>
</a><a href="http://feedads.pheedo.net/~a/DoZCMow">
<img src="http://feedads.pheedo.net/333.png"/>
</a>

but I want to keep all other link and images with donotremove .
Please help.:thumbsup:

venegal
05-03-2009, 02:30 AM
$string = <<<EOD
<a href="http://www.donotremove.com/"><img src="http://donotremove.net/~a/DoZC.jpg"/></a>
<img src="http://donotremove.net/22222.jpg"/>
<a href="http://feedads.g.doubleclick.net/~a/DoZCMoSsD67yAdsBepwwwwfo5M/0/da">
<img src="http://feedads.g.doubleclick.net/~a/DoZC.jpg"/>
</a><a href="http://feedads.pheedo.net/~a/DoZCMow">
<img src="http://feedads.pheedo.net/333.png"/>
</a>
EOD;

$regex = "/<a (?![^>]*donotremove).*<\/a>/Umis";
echo preg_replace($regex, "", $string);

roblogger
05-03-2009, 10:02 AM
thanks, but I need to use this to match and remove

<a href="http://feedads.g.doubleclick.net/~a/DoZCMoSsD67yAdsBepwwwwfo5M/0/da">
<img src="http://feedads.g.doubleclick.net/~a/DoZC.jpg"/>
</a><a href="http://feedads.pheedo.net/~a/DoZCMow">
<img src="http://feedads.pheedo.net/333.png"/>
</a>
because the other links can be in any form.

venegal
05-03-2009, 01:46 PM
Please be more specific. Do you want to remove all links except the ones containing "donotremove" or do you want to remove all links containing "doubleklick" and "pheedo" and keep all the other ones?

The snippet I gave you above does the former. If you want the latter, you have to change the regex to

$regex = "/<a (?=[^>]*(pheedo|doubleclick)).*<\/a>/Umis";

roblogger
05-04-2009, 09:41 PM
thank

$regex = "/<a (?=[^>]*(pheedo|doubleclick)).*<\/a>/Umis";
solved my problem.
this is what I gathered for removing ads from feeds :

$search = array('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@', // Strip multi-line comments including CDATA
'/<a (?=[^>]*(pheedo|doubleclick|feedburner|phdo|feeds)).*<\/a>/Umis',
'/<img (?=[^>]*(pheedo|doubleclick|feedburner|phdo|feeds|feeds2)).*\/>/Umis',
'/<div class="feedflare">.*?<\/div>/msi'
);
$text = preg_replace($search, '', $text);

it's not perfect but is all I can do with your help and google's



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum