note 37983 deleted from function.htmlentities by nlopess
| From: | nlopess@php.net | Date: | Wed, 05 May 2004 15:52:07 +0000 |
| Subject: | note 37983 deleted from function.htmlentities by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69253@lists.php.net to get a copy of this message | ||
Note Submitter: www.bitcetera.com (-sven)
----
This is a different approach to convert html entities while honoring tags. Given the following:
$string = "this is <b>a test</b> for tags & stuff like < this and <>
that";
Now you want to have the &, <, > encoded while "a test" should remain in bold.
Dodge this:
$encoded = implode('', preg_replace('/^([^<].+[^>])$/e',
"htmlentities('\\1')", preg_split('/(<.+?>)/', $string, -1,
PREG_SPLIT_DELIM_CAPTURE)));
It returns:
$encoded = "this is <b>a test</b> for tags & stuff like < this and
<> that";
This way, < and > not belonging to tags ARE encoded - unlike with the larger code sample a
little further down these notes.