note 54927 modified in function.htmlentities by danbrown

From: 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 &quot; 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 &copy; make &quot;me&quot; grin &reg; outputs Copyrights &#169; make &#34;me&#34; grin &#174; --was-- The existance of html entities such as &quot; 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 &copy; make &quot;me&quot; grin &reg; outputs Copyrights &#169; make &#34;me&#34; grin &#174; http://php.net/manual/en/function.htmlentities.php

« previous php.notes (#154927) next »