Mr B
Mr B

Reputation: 4130

PHP Regular Expression to replace html img tag with [IMG]

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.

Upvotes: 0

Views: 2831

Answers (4)

Harsh Patel
Harsh Patel

Reputation: 1344

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.';

Upvotes: 0

Alex
Alex

Reputation: 9481

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'.

Upvotes: 1

David Weitz
David Weitz

Reputation: 461

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

http://simplehtmldom.sourceforge.net/

Upvotes: 0

Shai Mishali
Shai Mishali

Reputation: 9402

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.

Upvotes: 0

Related Questions