December 4th, 2009
Fixing htmlentities for xml - 2
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
I am starting to use Google Chrome more often and doing this made me notice that one of my Javascript functions wasn’t working properly. A little bit of digging revealed that Chrome did not like the XML I was feeding it.
The code generating the XML was using the PHP function htmlentities to filter its output but this does not generate valid XML. Named entities such as " and & need to be represented by their Unicode values: " and &
A handy table with all the required replacements can be found at Carlos Ouro’s website.
Below is a simple function that replaces all the named HTML entities found in a string with their numeric HTML/XML counterparts.
Example Usage
$test = "" Hello World! "";
echo xmlentities($test) . PHP_EOL;
# Test #2
$test = "\"Hello World\"";
echo xmlentities( htmlentities($test) ) . PHP_EOL;
XMLEntities function
{
// Convert HTML entities to XML
// http://techtrouts.com/webkit-entity-nbsp-not-defined-convert-html-entities-to-xml/
$htmlentities = array( ""","&","<",">"," ","¡","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ","€");
$xmlentities = array(""","&","<",">"," ","¡","¢","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ý","þ","ÿ","€" );
// HTML entities are case-sensitive (http://htmlhelp.com/reference/html40/entities/)
return str_replace($htmlentities,$xmlentities,$string);
}


