homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Strip all attributes from a tag (except one i want to keep)

 7:34 pm on Oct 20, 2010 (gmt 0)

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"

$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/>



 7:52 pm on Oct 20, 2010 (gmt 0)

The simpilest way I can think would be to check for it. If it's there, then your tag is <p class='txt-center'></p> other wise your class is <p></p>

Something like this
//if we have the txt-center class check for " and '
if(stripos($tag,"class=\"txt-centered\"")===false) || stripos($tag,"class=\'txt-centered\'")===false)){
echo("<p class=\"txt-centered\">YourHtmlHere</p>");

Note, I didn't test that, there may be some syntax errors but the logic is sound.


 9:27 pm on Oct 20, 2010 (gmt 0)

The problem there (assuming i am reading the code correctly) is that it assumes only one paragraph tag per string...also, it does not account for the content within the paragraph (how would i plug the "YourHtmlHere" back in?...it would get overwritten.


 11:24 pm on Oct 20, 2010 (gmt 0)

$string = preg_replace("/<p[^[class=\"txt\-centered\"|>]]*(class=\"txt\-centered\")?[^>]*>/si", "<p \\1>", $string);


 12:06 am on Oct 21, 2010 (gmt 0)

Thanks. I will try that out tonight when i get home.


 5:06 pm on Oct 22, 2010 (gmt 0)

Did works?


 6:11 am on Oct 25, 2010 (gmt 0)

Hi NomikOS...i have not been able to try it yet. Swamped on something else now (i put another more complex approach in place for temporary, so problem has band-aid on it right now). I will post back here as soon as i can try it. Surely i will get back to it this week (i hope)!


 5:25 am on Dec 1, 2010 (gmt 0)

So I am sure nobody ever expected me to come back and respond to this...but here I am, and YES...IT WORKS PERFECTLY! THANKS!


 4:13 pm on Dec 1, 2010 (gmt 0)

cool. regular expressions are GREAT and not too much difficult indeed, but needs perseverance.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved