Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    preg_replace to remove links containing custom word

    I want to remove completely the links and images containing doubleclick and pheedo with preg_replace
    Code:
    <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.

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    PHP Code:
    $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); 

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, but I need to use this to match and remove
    Code:
    <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.

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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
    PHP Code:
    $regex "/<a (?=[^>]*(pheedo|doubleclick)).*<\/a>/Umis"

  • #5
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank
    Code:
    $regex = "/<a (?=[^>]*(pheedo|doubleclick)).*<\/a>/Umis";
    solved my problem.
    this is what I gathered for removing ads from feeds :
    PHP Code:
      $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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •