Preg_Replace Help

Discussion in 'PHP' started by dvancouver, Sep 23, 2008.

  1. #1
    I have a little coding I need help with and hope somebody can lend assistance.

    Here is what I am trying to do, currently this works very well but I need to not replace any image tag that has an alt tag in it. Thanks.

    preg_replace( "#<img(?:.+?)src=[\"'](\S+?)['\"][^>]+?>#is", "image holder", $comment );
     
    dvancouver, Sep 23, 2008 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe something like this?

    
    
    function replaceNotAlt ($alt){
      
      if (preg_match('#alt\s*=\s*([\'"])[^>]+\1#', $alt[0], $matches) )
      { 
        return $alt[0];
      }
      else
      { 
        // return what you want here
      }
    
    }
    
    $text = preg_replace_callback('#<img[^>]+?>#is', "replaceNotAlt", $text );
    
    PHP:
     
    LogicFlux, Sep 24, 2008 IP
    GTech likes this.