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.

Mr B
  • 3,980
  • 9
  • 48
  • 74
  • 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](http://stackoverflow.com/q/3577641) – Pekka Sep 19 '11 at 14:22
  • 1
    Is sample 1 itentionally corrupt HTML or did you mean `...width="175" height="80" />`? – cypher Sep 19 '11 at 14:24

4 Answers4

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

Alex
  • 9,313
  • 1
  • 39
  • 44
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.';
Harsh Patel
  • 1,032
  • 6
  • 21
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.

Shai Mishali
  • 9,224
  • 4
  • 56
  • 83
0

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

http://simplehtmldom.sourceforge.net/

David Weitz
  • 461
  • 4
  • 10
  • Suggested third party alternatives to [SimpleHtmlDom](http://simplehtmldom.sourceforge.net/) that actually use [DOM](http://php.net/manual/en/book.dom.php) instead of String Parsing: [phpQuery](http://code.google.com/p/phpquery/), [Zend_Dom](http://framework.zend.com/manual/en/zend.dom.html), [QueryPath](http://querypath.org/) and [FluentDom](http://www.fluentdom.org). – Gordon Sep 23 '11 at 09:12