PHP – 正则表达式删除事件属性的所有出现
发布时间:2020-12-13 17:26:42 所属栏目:PHP教程 来源:网络整理
导读:经过几个小时的尝试,我在这里问.我想从POSTed文本中删除所有js事件属性和样式属性的出现.它可能包含也可能不包含新行. 发表示例文字: a href="http://www.google.com" onclick="unwanted_code" style="unwanted_style" ondblclick="unwanted_code" onmouSEO
经过几个小时的尝试,我在这里问.我想从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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |