简体   繁体   中英

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.

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

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

eg

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

Shai.

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

http://simplehtmldom.sourceforge.net/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM