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.
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:Notice
\\\?. We escape the backslash and question marl (with a backslash) and then say ‘this character is optional’.