0

I have the following regular expression to replace an html img tag with [IMG];

echo preg_replace('/(^<img) (.*) (>$)/i', '[IMG]', $subject);

It works as expected to a certain extent, however some of the img tags I'm working end with '/>' and some end with '>'. I can't get the above to work with the latter.

Sample 1 (works):

<img src="image-1.gif" alt="image-1" width="175>" height="80" />

Sample 2 (doesn't work)

<img src="image-2.gif" width="77" height="51" alt="image-2">

Appreciate the help.

2
  • 3
    That's why regular expressions are not optimal for parsing PHP. Consider using a HTML parser instead. Best methods to parse HTML with PHP Commented Sep 19, 2011 at 14:22
  • 1
    Is sample 1 itentionally corrupt HTML or did you mean ...width="175" height="80" />? Commented Sep 19, 2011 at 14:24

4 Answers 4

1

Although Pekka is right to say you should use an HTML parser (I completely agree), for education's sake, you can use the 'optional' character, ?, which marks the previous character as optional:

echo preg_replace('/(^<img) (.*)(\\\?>$)/i', '[IMG]', $subject);

Notice \\\?. We escape the backslash and question marl (with a backslash) and then say 'this character is optional'.

Sign up to request clarification or add additional context in comments.

1 Comment

My bad, needed an extra escape. Works now.
0

I would suggest fetching the URL and then manually writing the [IMG] tag.

e.g.

preg_match('/src="(.*?)"/', '<img src="image-2.gif" width="77" height="51" alt="image-2">', $matches)
echo '[IMG]'.$matches[1].'[/IMG]';

Shai.

Comments

0

I would try to use a DOM parser. They're much more reliable.

http://simplehtmldom.sourceforge.net/

1 Comment

Suggested third party alternatives to SimpleHtmlDom that actually use DOM instead of String Parsing: phpQuery, Zend_Dom, QueryPath and FluentDom.
0

for example we have string like this :- $str = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.

so then we can do replace img tag like below

STEP 1

$str = 'Text <img src="hello.png" > hello <img src="bye.png" /> other text.';
if(preg_match("/(<img .*>)/i", $str)){
     $img_array = preg_split('/(<img .*>)/i', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
}

This will output:

array(5) {
  [0]=>
  string(5) "Text "
  [1]=>
  string(22) "<img src="hello.png" >"
  [2]=>
  string(7) " hello "
  [3]=>
  string(21) "<img src="bye.png" />"
  [4]=>
  string(12) " other text."
}

STEP 2 then we will to do replace in for loop

for ($i = 0; $i < count($img_array); $i++){
     $url = "welcome.png";
     $img_array[$i] = preg_replace('/(<img .*>)/i', '<img src="'.$url.'" alt="'.$url.'">', $img_array[$i]); //replace src path & alt text
}

STEP 3 then after convert array to string

$str = implode('', $img_array);

then after you will get final output like this

$str = 'Text <img src="welcome.png" > hello <img src="welcome.png" /> other text.';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.