简体   繁体   English

如何在空 src 或没有 src 时删除图像标签

[英]How to remove image tag when empty src or without src

$content = '<img src=""><img alt="test" width="400px"><a href="https://placeholder.com"><img src="https://via.placeholder.com/350x150"></a>';
$content = preg_replace('!(<a\s[^>]+>)?<img([^>]+)src=""([^>]*)>(</a>)?!i' , '' , $content );
echo $content;

But only able to remove src="" , can't remove without src但只能删除src="" ,没有 src 不能删除

<img alt="test" width="400px"><a href="https://placeholder.com"><img src="https://via.placeholder.com/350x150"></a>

Use the regex, <img(?![^>]*(src="[^"]+"))[^>]*> .使用正则表达式<img(?![^>]*(src="[^"]+"))[^>]*>

The negative lookahead (?![^>]*(src="[^"]+") will not capture the img tag with src="..."负向前瞻(?![^>]*(src="[^"]+")不会捕获带有src="..."的 img 标签

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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