strip all classes from p tags

SoulieBaby picture SoulieBaby · Jul 23, 2009 · Viewed 13.3k times · Source

I was just wondering if any one knew a function to remove ALL classes from a string in php.. Basically I only want

<p> 

tags rather than

<p class="...">

If that makes sense :)

Answer

Paul Dixon picture Paul Dixon · Jul 23, 2009

A fairly naive regex will probably work for you

$html=preg_replace('/class=".*?"/', '', $html);

I say naive because it would fail if your body text happened to contain class="something" for some reason!. It could be made a little more robust by looking for class="" inside angled bracketted tags if need be.