我对我们的产品有一些描述,但遗憾的是,这是一个遗留代码,有很多html元素,我真的不需要。我用strip_tags
删除了这些,但我需要保留图像。不幸的是,以前的设计是如此古老,包含了这样的字符串:
<IMG border=0 alt="" src="http:///Images/white.gif" width=5 height=1>
<IMG border=0 alt="" src="http:///Images/white.gif" width=170 height=1>
<img src="/asdsa/alkatreszkepek/aasdsa.jpg">
<IMG border=0 alt="" src="http:///Images/white.gif" width=170 height=1>
<IMG border=0 alt="" src="http:///Images/white.gif" width=2 height=1>
我需要替换所有的图像,在src标签中不包含alkatreszkepek
(表示产品图片(的图像。
到目前为止我尝试了什么:
首先只得到一个图像:
<img.*?src=".*?".*?>
很酷,所以我用的是isg
修饰语,它是所有的图像。
现在我试图否定:
<img.*?src=".*?^(?!.alkatreszkepek).*?".*?>
但是在这种情况下将不选择任何内容。
有人能帮我吗,我怎么得到那些不必要的图像?
我想在PHP中将它们preg_replace
为零。
编辑
是的,我知道,我可以用preg_match_all
获得所有图像,然后迭代匹配,然后轻松地str_replace
。我只想知道,我如何用regex做到这一点。
这应该可以工作(但效率不是很高(:
(?:<img[^>]+src's*='s*(["'])(((?!alkatreszkepek).)*?))'1[^>]*>
上面的regex将匹配任何img,不包含src属性内的(子(字符串"alkarteskepek"(处理不同的引号"
或'
(
组#2中的此版本与图像url 匹配
此处测试:https://regex101.com/r/dE5vS7/8