note 97008 added to function.json-encode

From: Date: Sat, 27 Mar 2010 21:36:06 +0000
Subject: note 97008 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-167454@lists.php.net to get a copy of this message
I came across the "bug" where running json_encode() over a SimpleXML object was ignoring the CDATA. I ran across http://bugs.php.net/42001 and http://bugs.php.net/41976, and while I agree with the poster that the documentation should clarify gotchas like this, I was able to figure out how to workaround it. You need to convert the SimpleXML object back into an XML string, then re-import it back into SimpleXML using the LIBXML_NOCDATA option. Once you do this, then you can use json_encode() and still get back the CDATA. <?php // Pretend we already have a complex SimpleXML object stored in $xml $json = json_encode(new SimpleXMLElement($xml->asXML(), LIBXML_NOCDATA)); ?> ---- Server IP: 208.69.120.55 Probable Submitter: 24.22.140.252 ---- Manual Page -- http://www.php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/97008 Del: integrated -- https://master.php.net/note/delete/97008/integrated Del: useless -- https://master.php.net/note/delete/97008/useless Del: bad code -- https://master.php.net/note/delete/97008/bad+code Del: spam -- https://master.php.net/note/delete/97008/spam Del: non-english -- https://master.php.net/note/delete/97008/non-english Del: in docs -- https://master.php.net/note/delete/97008/in+docs Del: other reasons-- https://master.php.net/note/delete/97008 Reject -- https://master.php.net/note/reject/97008 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#167454) next »