note 54927 modified in function.htmlentities by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 17:02:31 +0000 |
| Subject: | note 54927 modified in function.htmlentities by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154927@lists.php.net to get a copy of this message | ||
The existance of html entities such as " inside an xml node causes most xml parsers to
throw an error. The following function cleans an input string by converting html entities to valid
unicode entities.
<?php
function htmlentities2unicodeentities ($input) {
$htmlEntities = array_values (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
$entitiesDecoded = array_keys (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
$num = count ($entitiesDecoded);
for ($u = 0; $u < $num; $u++) {
$utf8Entities[$u] = '&#'.ord($entitiesDecoded[$u]).';';
}
return str_replace ($htmlEntities, $utf8Entities, $input);
}
?>
So, an input of
Copyrights © make "me" grin ®
outputs
Copyrights © make "me" grin ®
--was--
The existance of html entities such as " inside an xml node causes most xml parsers to
throw an error. The following function cleans an input string by converting html entities to valid
unicode entities.
<?php
function htmlentities2unicodeentities ($input) {
$htmlEntities = array_values (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
$entitiesDecoded = array_keys (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
$num = count ($entitiesDecoded);
for ($u = 0; $u < $num; $u++) {
$utf8Entities[$u] = '&#'.ord($entitiesDecoded[$u]).';';
}
return str_replace ($htmlEntities, $utf8Entities, $input);
}
?>
So, an input of
Copyrights © make "me" grin ®
outputs
Copyrights © make "me" grin ®
http://php.net/manual/en/function.htmlentities.php