note 95447 deleted from function.simplexml-load-string by danbrown

From: Date: Mon, 04 Jan 2010 16:03:37 +0000
Subject: note 95447 deleted from function.simplexml-load-string by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-164613@lists.php.net to get a copy of this message
Note Submitter: webmaster at netsupportchat dot com ---- I had a heck of a time getting a small XML-based CMS to work. My pages looked like this <page> <title>Example</title> <content> <div style="blah.."> Valid XHTML goes here </div> </content> </page> but everytime I ran simplexml_load_string() on my XML data, I got errors like: Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 15: parser error : Entity 'nbsp' not defined in /var/www/dev... I searched around and found no information except for a bogus bug report in bugs.php.net that explained nothing. I found two solutions: Solution #1: use LIBXML_DTDLOAD in the options and define the XHTML doctype, e.g. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> Although that will require going out and loading the DTD each page load. Pretty poor. To remedy this I just copied the XHTML DTD locally and used it like so: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "xml/dtd/xhtml1-strict.dtd"> If you have the DTD and related files in xml/dtd/ .. For the XHTML Strict DTD there are 4 files total, you have to get all of them locally. The files are: xhtml-lat1.ent, xhtml-special.ent, xhtml-symbol.ent, xhtml1-strict.dtd Or wait, solution #2 (hacky but FASTER): <?php $xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOERROR); ?> I developed the site with solution #1 but once I went live I switched it to the second solution I gave. Also, another quick note, to combine the options AFAIK, use | not & like I assumed. I don't know much about bitwise, but this worked for me when combining two options: <?php $xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_COMPACT); ?> Thanks, Ben Carroll @ http://www.netsupportchat.com

« previous php.notes (#164613) next »