php string replacement

Discussion in 'PHP' started by squishi, Sep 26, 2008.

  1. #1
    I have a string that contains image tags.
    I need to add a width to all image tags, except if they are smilie images.

    So an image like this on should get a width added:
    <img src="someurl...
    should become:
    <img width="200" src="someurl...
    PHP:
    Smilie-images will look like this in the string:
    <img src="/images/smilies/someurl..
    PHP:
    They should not get the width added.

    How can I do this?
    Adding the width tag to the images is easy with replacements. But it also adds it to the smiley images. :eek:
     
    squishi, Sep 26, 2008 IP
  2. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You use preg_match to see if "smilies" exists in the string (I don't know the code off hand for this, just lookup the function on php.net).

    Then something like this:

    
    if(!preg_match(whatever parameters)) {
      $imgcode = ereg_replace("<img", "<img width=\"200\"", $imgcode);
    }
    
    PHP:
    I may have the parameters mixed up for ereg_replace, but that's the basics of how to do it.
     
    Christian Little, Sep 26, 2008 IP
  3. squishi

    squishi Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yeah, okay. But the string can contain more than one image tag.
    So I need to go through the tags one by one and check if it's a smilie or not...
     
    squishi, Sep 26, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    
    $code = preg_replace('#<img src="([/images/smilies]^)">#iUe','<img width="200" src="\\l">',$code);
    
    PHP:
    Not sure if the ^ should be before or after to exclude.

    Peace,
     
    Barti1987, Sep 26, 2008 IP