Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Adapted from <a href="https://stackoverflow.com/questions/2994448/regex-strip-html-attributes-except-src/3001582#3001582">my answer on a similar question</a></p> <pre><code>$text = '&lt;p style="padding:0px;"&gt;&lt;strong style="padding:0;margin:0;"&gt;hello&lt;/strong&gt;&lt;/p&gt;'; echo preg_replace("/&lt;([a-z][a-z0-9]*)[^&gt;]*?(\/?)&gt;/i",'&lt;$1$2&gt;', $text); // &lt;p&gt;&lt;strong&gt;hello&lt;/strong&gt;&lt;/p&gt; </code></pre> <p>The RegExp broken down:</p> <pre><code>/ # Start Pattern &lt; # Match '&lt;' at beginning of tags ( # Start Capture Group $1 - Tag Name [a-z] # Match 'a' through 'z' [a-z0-9]* # Match 'a' through 'z' or '0' through '9' zero or more times ) # End Capture Group [^&gt;]*? # Match anything other than '&gt;', Zero or More times, not-greedy (wont eat the /) (\/?) # Capture Group $2 - '/' if it is there &gt; # Match '&gt;' /i # End Pattern - Case Insensitive </code></pre> <p>Add some quoting, and use the replacement text <code>&lt;$1$2&gt;</code> it should strip any text after the tagname until the end of tag <code>/&gt;</code> or just <code>&gt;</code>.</p> <p><strong>Please Note</strong> This isn't necessarily going to work on <em>ALL</em> input, as the Anti-HTML + RegExp will tell you. There are a few fallbacks, most notably <code>&lt;p style="&gt;"&gt;</code> would end up <code>&lt;p&gt;"&gt;</code> and a few other broken issues... I would recommend looking at <a href="http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Filter/StripTags.php" rel="noreferrer">Zend_Filter_StripTags</a> as a more full proof tags/attributes filter in PHP</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload