Skip to content Skip to sidebar Skip to footer

Remove On* Js Event Attributes From Html Tags

Please, help to parse in PHP simple html strings (php regexp). I need drop html-js events from html code. I know php regular expressions very bad. Examples of code:

Example usage

Online example:

$str = '
<buttononclick="..javascript instruction.."><buttononclick="..javascript instruction.."value=".."><buttononclick=..javascript_instruction..><buttononclick=..javascript_instruction..value><helloword "" ontest = "hai"x="y"onfoo=bar/baz  />
';

echo $str . "\n----------------------\n";

echo remove_event_attributes($str);

Output:

<buttononclick="..javascript instruction.."><buttononclick="..javascript instruction.."value=".."><buttononclick=..javascript_instruction..><buttononclick=..javascript_instruction..value><helloword "" ontest = "hai"x="y"onfoo=bar/baz  />

----------------------

<button ><buttonvalue=".."><button ><buttonvalue><helloword "" x="y"   />

Solution 2:

You might be better off using DOMDocument.

You can use it to iterate over the DOM tree represented by the HTML file you're trying to parse, looking for the various on* attributes that you want to remove.

This approach is more likely to succeed because DOMDocument actually understands the semantics of a HTML file, whereas regex is just a dumb string parser and inadequate for reliably parsing HTML.

Post a Comment for "Remove On* Js Event Attributes From Html Tags"