Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7567475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:40:21+00:00 2026-05-30T14:40:21+00:00

Please, help to parse in PHP simple html strings (php regexp). I need drop

  • 0

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:

<button onclick="..javascript instruction..">

Result: <button>

<button onclick="..javascript instruction.." value="..">

Result: <button value="..">

<button onclick=..javascript instruction..>

Result: <button>

<button onclick=..javascript instruction.. value>

Result: <button value>

I need to do this without quotes and with, because all modern browsers is allow to do attributes without quoutes.

Note: I nedd parse not only onclick.. it is all atrributes, which begins from ‘on’.

Note (2): DONT TRY TO ADVICE HTML PARSER, BECAUSE IT WILL BE VERY BIG DOM TREE FOR PARSE..

UPDATED: Thanks, for your reply! Now, i use HTMLPurifier component on written by me a small framework.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:40:23+00:00Added an answer on May 30, 2026 at 2:40 pm

    There is nothing wrong with tokenizing with regex. But making a full HTML tokenizer with regex is a lot of work and difficult to get right. I would recommend using a proper parser, because you will probably need to remove script tags and such anyway.

    Assuming a full tokenizer is not needed, the following regex and code can be used to remove on* attributes from HTML tags.
    Because a proper tokenizer is not used, it would match strings that look like tags even in scripts, comments, CDATA, etc.

    There is no guarantee that all event attributes will be removed for all input/browser combinations! See the notes below.

    Note on error tolerance:

    Browsers are usually forgiving of errors.
    Due to that it is difficult to tokenize tags and get the attributes as the browser would see them when “invalid” data is present.
    Because error tolerance and handling differs between browsers it is impossible to make a solution that works for them all in all cases.

    Thus: Some browser(s) (current, past, or future version) could treat something which my code does not think is a tag, as a tag, and execute the JS code.

    In my code I have attempted to mimic tokenization of tags (and error tolerance/handling) of recent Google Chrome versions.
    Firefox seems to do it in a similar way.

    IE 7 differs, in some cases it’s not as tolerant (which is better than if it was more tolerant).
    (IE 6 – lets not go there. See XSS Filter Evasion Cheat Sheet)

    Relevant links:

    • HTML5 Tokenization
    • XSS Filter Evasion Cheat Sheet

    The code

    $redefs = '(?(DEFINE)
        (?<tagname> [a-z][^\s>/]*+    )
        (?<attname> [^\s>/][^\s=>/]*+    )  # first char can be pretty much anything, including =
        (?<attval>  (?>
                        "[^"]*+" |
                        \'[^\']*+\' |
                        [^\s>]*+            # unquoted values can contain quotes, = and /
                    )
        ) 
        (?<attrib>  (?&attname)
                    (?: \s*+
                        = \s*+
                        (?&attval)
                    )?+
        )
        (?<crap>    [^\s>]    )             # most crap inside tag is ignored, will eat the last / in self closing tags
        (?<tag>     <(?&tagname)
                    (?: \s*+                # spaces between attributes not required: <b/foo=">"style=color:red>bold red text</b>
                        (?>
                            (?&attrib) |    # order matters
                            (?&crap)        # if not an attribute, eat the crap
                        )
                    )*+
                    \s*+ /?+
                    \s*+ >
        )
    )';
    
    
    // removes onanything attributes from all matched HTML tags
    function remove_event_attributes($html){
        global $redefs;
        $re = '(?&tag)' . $redefs;
        return preg_replace("~$re~xie", 'remove_event_attributes_from_tag("$0")', $html);
    }
    
    // removes onanything attributes from a single opening tag
    function remove_event_attributes_from_tag($tag){
        global $redefs;
        $re = '( ^ <(?&tagname) ) | \G \s*+ (?> ((?&attrib)) | ((?&crap)) )' . $redefs;
        return preg_replace("~$re~xie", '"$1$3"? "$0": (preg_match("/^on/i", "$2")? " ": "$0")', $tag);
    }
    

    Example usage

    Online example:

    $str = '
    <button onclick="..javascript instruction..">
    <button onclick="..javascript instruction.." value="..">
    <button onclick=..javascript_instruction..>
    <button onclick=..javascript_instruction.. value>
    <hello word "" ontest = "hai"x="y"onfoo=bar/baz  />
    ';
    
    echo $str . "\n----------------------\n";
    
    echo remove_event_attributes($str);
    

    Output:

    <button onclick="..javascript instruction..">
    <button onclick="..javascript instruction.." value="..">
    <button onclick=..javascript_instruction..>
    <button onclick=..javascript_instruction.. value>
    <hello word "" ontest = "hai"x="y"onfoo=bar/baz  />
    
    ----------------------
    
    <button >
    <button  value="..">
    <button >
    <button  value>
    <hello word "" x="y"   />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please help me to parse xml from another xml... i have this xml named
How should parse with PHP (simple html dom/etc..) background and other images of webpage?
I bring the code from google to correct it fit for my need The
Possible Duplicate: How to parse and process HTML with PHP? Here is what I'm
I encoded a mysql result from php to json.. I need to decode it
I need some help with creating a regex for my php script. Basically, I
Possible Duplicate: How to parse HTML with PHP? Grabbing the href attribute of an
I am using php imap functions to parse the message from webmail. I can
Can anyone please help me to add video controls (play, pause, forward, seekbar) to
Please help! I'm really at my wits' end. My program is a little personal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.