PHP preg_replace:remove style =" .."从img标签 [英] PHP preg_replace: remove style=".." from img tags

查看:169
本文介绍了PHP preg_replace:remove style =" .."从img标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个preg_replace的表达式,删除图像的所有内联CSS样式。
例如,我有这样的文字:

I'm trying to find an expression for preg_replace, that deletes all inline css styles for images. For example, I have this text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. <img style="float:left; margin:0 0 10px 10px;" src="image.jpg" /> Proin vestibulum libero id nisl dignissim eu sodales.

我需要使它看起来像:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. <img src="image.jpg" /> Proin vestibulum libero id nisl dignissim eu sodales.

我已经尝试过很多表达式,例如

I have tried dozens of expressions like

preg_replace("%<img(.*?)style(.*?)=(.*?)(\'|\")(.+?)(\'|\")(.*?)>%i", "<img\$1\$7>", $article->text)

但无效。有任何建议吗?

but nothing worked. Any suggestions?

推荐答案

preg_replace('/(\<img[^>]+)(style\=\"[^\"]+\")([^>]+)(>)/', '${1}${3}${4}', $article->text)

这可以帮助

这篇关于PHP preg_replace:remove style =&quot; ..&quot;从img标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆