Welcome to WebmasterWorld Guest from 54.167.157.247

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)

5+ Year Member



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)

WebmasterWorld Senior Member 5+ Year Member



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)

5+ Year Member



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)

10+ Year Member



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

5+ Year Member



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

10+ Year Member



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

5+ Year Member



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)

5+ Year Member



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)

10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month