I am trying to find a php function that will remove all attributes from the paragraph <p> tag of a string except for one. When people enter info into a form having pasted it from Word, it often has various font and other styling associated with it. These are applied within the <p> tag. I had found a script that would strip ALL attributes from whatever tag is specified, but the problem is that there is ONE (only one) attribute that can be optionally used. It will not necessarily be applied to ALL <p> tags, but might be. If it is there, i want to keep it. Otherwise, i want to return an empty paragraph tag. I then found an implementation using DOMDocument, loadHTML, and getElementsByTagName...which works perfectly, except the output now includes header tags and encloses the whole string in HTML tags as if it were a standalone HTML page instead of a snippet.
I assume this can be accomplished with some sort of regex and loop, but this is WAY over my head. Anyone able to help?
The one allowed attribute is: class="txt-centered"
Example:
$string = <p class="unwanted" style="text-align: center;">this is</p><p>some text about</p><p class="txt-centered">something fun<p/>
I want this to return:
$string = <p>this is</p><p>some text about</p><p class="txt-centered">something fun<p/>