I need remove all img element in html that non contain src attribute with preg_match and PHP
something like:
<html>
<img src="someurl" alt="something" />
<img alt="something" />
<html />
in
<html>
<img src="someurl" alt="something" />
<html />
Tanks
In case your boss insists on a regex, and (s)he does not hear to the voice of wisdom, you can try the following regex:
(?si)\s*<img\b(?>(?!src=).)*?\/>\s*
See demo on regex101 .
Sample PHP code:
$re = "/(?si)\\s*<img\\b(?>(?!src=).)*?\\/>\\s*/";
$str = "<html>\n <img src=\"someurl\" alt=\"something\" />\n <img alt=\"something\" />\n <img alt=\"somethingelse\"\n att='val' />\n<html />";
$result = preg_replace($re, "", $str);
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.