Martijn's PHP Coding Blog


December 8th, 2009

My Taipei Zend / PHP 5 Exam Notes - 6

taipei 101 by orange tuesday @ flickr

taipei 101 by orange tuesday @ flickr

I have been busy these last couple of weeks preparing for the Zend Certified Engineer / PHP5 exam. Although I have been using PHP for years – I never had a need to “prove” this. That might change in a few months as I start looking for a new job. Doing the Zend exam in preparation of this sounded like a good start. My first step was to order the Zend PHP5 Certification Study Guide by Davey Shafik and Ben Ramsey from Amazon.

The difficulty is not so much in the technologies — most things in PHP are very straightforward — but the sheer scope of what PHP covers and the potential for in dept questions. The exam covers programming basics such as converting hex / octal and decimal, XML & XPath, Web programming, OOP, Databases, Pattern theory etc. The study guide covers most topics but it alone is not enough to pass the exam. It will set you on the right path — but you really need to dive deeper. Read the rest of this entry »

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