脚本宝典收集整理的这篇文章主要介绍了PHP – 正则表达式删除事件属性的所有出现,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
发表示例文字:
<a href="http://www.GOOGLE.COM" onclick="unwanted_code" style="unwanted_style" ondblclick="unwanted_code" onmouSEOver="unwanted_code">google</a> is a seArch engine. There are other engines too. <a href="http://www.yahoo.com" onclick="unwanted_code" ondblclick="unwanted_code" onmouSEOver="unwanted_code" style="unwanted_style">yahoo</a> is another engine.
第一次尝试:
$pattern[0] = '/(<[^>]+) on.*=".*?"/iU'; $replace[0] = '$1'; $pattern[1] = '/(<[^>]+) style=".*?"/iU'; $replace[1] = '$1'; $out = PReg_replace($pattern,$replace,$in);
输出:
<a href="http://www.google.com">yahoo</a> is another engine.
第二次尝试:
$out = preg_replace_callback('/(<[^>]+) on.*=".*?"/iU',function($m) {return $m[1];},$in);
输出:
<a href="http://www.google.com">yahoo</a> is another engine.
输出我想要的是:
<a href="http://www.google.com">google</a> is a search engine. There are other engines too. <a href="http://www.yahoo.com">yahoo</a> is another engine.
有人帮帮我吗?
$content = '<a href="http://www.google.com" onclick="unwanted_code" style="unwanted_style" ondblclick="unwanted_code" onmouSEOver="unwanted_code">google</a> is a search engine. There are other engines too. <a href="http://www.yahoo.com" onclick="unwanted_code" ondblclick="unwanted_code" onmouSEOver="unwanted_code" style="unwanted_style">yahoo</a> is another engine.'; $result = preg_replace('%(<a href="[^"]+")[^>]+(>)%m',"$1$2",$content); echo $result,"\n";
输出:
<a href="http://www.google.com">google</a> is a search engine. There are other engines too. <a href="http://www.yahoo.com">yahoo</a> is another engine.
以上是脚本宝典为你收集整理的PHP – 正则表达式删除事件属性的所有出现全部内容,希望文章能够帮你解决PHP – 正则表达式删除事件属性的所有出现所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。