...

View Full Version : PHP returns invalid XML because of '&'



mesh2005
08-27-2007, 11:05 AM
I'm writing a PHP script to query a database table and return the result as XML. If the result contain the letter '&', the XML is corrupted. How can I fix this problem?
<xml>
<record id='2' text='jjjj&kkkk'>any</record>
</xml>

Bill Posters
08-27-2007, 11:10 AM
Encode it as &amp;

Bill Posters
08-27-2007, 11:23 AM
Encode it as &amp; in the stored XML file.

e.g.

<xml>
<record id='2' text='jjjj&amp;kkkk'>any</record>
</xml>

The htmlspecialchars (http://uk.php.net/manual/en/function.htmlspecialchars.php) and htmlentities (http://uk.php.net/manual/en/function.htmlentities.php) won't always do the job properly, so I use my owned, tweaked optionů


function xmlspecialchars($str) {

return str_replace('&#38;#039;','&apos;',htmlspecialchars($str,ENT_QUOTES,'UTF-8'));

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum