Martijn's PHP Coding Blog


December 4th, 2009

Fixing htmlentities for xml - 2

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.

Entity '&iexcl' not defined

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 #1
       $test = "" Hello World! "";
        echo xmlentities($test) . PHP_EOL;

        # Test #2
       $test = "\"Hello World\"";
        echo xmlentities( htmlentities($test) ) . PHP_EOL;

XMLEntities function

function xmlentities($string)
{

    // 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);
}

Tags: ,

2 Responses to “Fixing htmlentities for xml”

  1. KIRK Says:


    Medicamentspot.com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. Low price pills. Buy drugs online

    Buy:Seroquel.Acomplia.Female Pink Viagra.Ventolin.Advair.Lipitor.Lasix.Buspar.Wellbutrin SR.Zocor.Female Cialis.Nymphomax.Zetia.Amoxicillin.Lipothin.Cozaar.SleepWell.Prozac.Aricept.Benicar….

  2. BEN Says:


    Medicamentspot.com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. Low price pills. Order drugs online

    Buy:Viagra Soft Tabs.Soma.Cialis Super Active+.Cialis.Maxaman.Tramadol.Viagra Super Force.Propecia.Zithromax.Viagra Professional.VPXL.Cialis Professional.Viagra Super Active+.Viagra.Levitra.Super Active ED Pack.Cialis Soft Tabs….

Leave a Reply