PHP – 正则表达式删除事件属性的所有出现

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP – 正则表达式删除事件属性的所有出现脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
经过几个小时的尝试,我在这里问.我想从POSTed文本中删除所有js事件属性和样式属性的出现.它可能包含也可能不包含新行.

发表示例文字

<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,请注明来意。