Re: Stripping HTML selectively?
| From: | Steve Edberg | Date: | Sun, 04 Mar 2001 11:06:40 +0000 |
| Subject: | Re: Stripping HTML selectively? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-42494@lists.php.net to get a copy of this message | ||
At 09:43 PM 3/3/01 , Erick Papadakis wrote:
Thanks Brian, I have tried the allowable tags, but I need to remove the ATTRIBUTES of a tag, not the tag itself. STRIP_TAGS totally removes the tag, and ALLOWABLE_TAGS lets the tag be. WHat I wish to do is let the main tag be but remove its attributes, as follows:Well, in this case, you'd have to use regular expressions. One way to do it would be:Original text: <font class="something" style="....">Hi!</font>Parsed text: <font>Hi!</font>Thanks/erick
$SanitizedString = ereg_replace('<[[:space:]]*([[:alnum:]]+)[^>]*>', "<\\1>", $String);
this _should_ work (haven't tested it). If you wanted to remove some tags entirely, and then remove the attributes of the remaining tags, you could (1) use strip_tags() with a list of allowable tags, then (2) run the regexp above. Incidentally, the above regexp also removes leading spaces from the tag. Eg, < font style="unreadable"> becomes <font>. If you don't want, that user the regexp
'<([[:space:]]*[[:alnum:]]+)[^>]*>'instead.
- steve
--- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.230 / Virus Database: 111 - Release Date: 25-Jan-01+------------------------------------------------------------------------+
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu (530)754-9127 | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |+---------------------- Gort, Klaatu barada nikto! ----------------------+