Welcome to WebmasterWorld Guest from 54.221.9.209

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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

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

Junior Member

5+ Year Member

joined:May 12, 2007
posts: 91
votes: 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"

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/>
7:52 pm on Oct 20, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:June 10, 2008
posts: 1130
votes: 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>");
}
else{
echo("<p>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)

Junior Member

5+ Year Member

joined:May 12, 2007
posts: 91
votes: 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)

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts: 185
votes: 0


$string = preg_replace("/<p[^[class=\"txt\-centered\"|>]]*(class=\"txt\-centered\")?[^>]*>/si", "<p \\1>", $string);
12:06 am on Oct 21, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:May 12, 2007
posts: 91
votes: 0


Thanks. I will try that out tonight when i get home.
5:06 pm on Oct 22, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts: 185
votes: 0


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

Junior Member

5+ Year Member

joined:May 12, 2007
posts: 91
votes: 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)

Junior Member

5+ Year Member

joined:May 12, 2007
posts: 91
votes: 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)

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts: 185
votes: 0


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