SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: regex inverse

  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex inverse

    PHP Code:
    $a 'asdasdasd as asdasdas <img src="http://www.domain1.com/1.gif"> sdfsdf <img src="http://www.domain2.com/1.gif">  <img src="http://www.domain3.com/1.gif">';

    $pattern '(<img[^>]+domain1.com[^>]+>)';
    $a preg_replace($pattern''$a); 
    How can I keep only img tags from domain1.com?

    Any idea for an inverse like
    $pattern = '!(<img[^>]+domain1.com[^>]+>)';

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    function domain_filter($match$domain 'domain1.com'){
      if(
    stristr($match[0], $domain)){
        return 
    $match[0];
      }
    }

    $filtered preg_replace_callback(
      
    '~<img[^>]+>~',
      
    'domain_filter',
      
    'asdasdasd as asdasdas <img src="http://www.domain1.com/1.gif"> sdfsdf <img src="http://www.domain2.com/1.gif">  <img src="http://www.domain3.com/1.gif">'
    );

    echo 
    $filtered;
    /* 
      asdasdasd as asdasdas <img src="http://www.domain1.com/1.gif"> sdfsdf   
    */
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

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