after hours of trying, I'm here to ask. I want to remove all occurrences of js event attributes and style attribute from POSTed text. it may or may not contain newlines .
Sample text with text:
<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.
first try:
$pattern[0] = '/(<[^>]+) on.*=".*?"/iU';
$replace[0] = '$1';
$pattern[1] = '/(<[^>]+) style=".*?"/iU';
$replace[1] = '$1';
$out = preg_replace($pattern, $replace, $in);
exit:
<a href="http://www.google.com">yahoo</a> is another engine.
second attempt:
$out = preg_replace_callback('/(<[^>]+) on.*=".*?"/iU', function($m) {return $m[1];}, $in);
exit:
<a href="http://www.google.com">yahoo</a> is another engine.
The output I'm trying to get is:
<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.
Does anyone help me?
source
share