Search code examples
simplexmlphpentities

PHP simplexml Entities


What's going one here?

$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
    <album>
        <img src="002.jpg" caption="w&aacute;ssup?" />
    </album>
XML;

$xml = simplexml_load_string($string);
// $xmlobj = simplexml_load_file("xml.xml"); // same thing

echo "<pre>";
var_dump($xml);
echo "</pre>";

Error:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 5: parser error : Entity 'aacute' not defined


Solution

  • &aacute is not an XML entity - you're thinking about HTML.

    Special characters are usually used "as is" in XML - an html_entity_decode() on the input data (don't forget to specify UTF-8 as the character set) should do the trick:

    $string = html_entity_decode($string, ENT_QUOTES, "utf-8");