note 42143 deleted from function.htmlentities by nlopess
| From: | nlopess@php.net | Date: | Wed, 05 May 2004 15:52:33 +0000 |
| Subject: | note 42143 deleted from function.htmlentities by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69254@lists.php.net to get a copy of this message | ||
Note Submitter: www.bitcetera.com (-sven)
----
(Please ignore my post further down, it is buggy.)
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. However, as this line of code does not
actually check if a valid HTML keyword is between the < and >, the string "5 is < than
7 and not > 9" will not be encoded, as " than 7 and not " is falsely taken for a
HTML tag.