It results in illegal JSON when trying to parse with jQuery. The entire document, database connection is encoding in UTF-8.
The code
header('Content-Type: application/json; charset=utf-8');
echo (json_encode($products));
This is what is output (Updated):
{
"D8": {
"title": "Green A\/C U\/V Dye Self-Measure bottle treats 32 véhicules 8 oz. (237 ml)",
"image": "http:\/\/www.supercool.ac\/wp-content\/uploads\/2011\/10\/D8.png",
"Description": "<ul>\n<li>Efficace dans le R-134a et R-12 systèmes de réfrigération<\/li>\n<li>Detectable when exposed to U\/V light<\/li>\n<li>Compatible avec PAG, ester, les huiles minérales et<\/li>\n<li>Ne contient pas de solvants!<\/li>\n<li>Will not harm A\/C systems or recovery equipment<\/li>\n<li>Universal A\/C Dye Safe for Hybrid and<br \>\n Véhicules électriques<\/li>\n<li>Partie # D8<\/li>\n<\/ul>\n"
}
}
Shouldn’t this be escaped by default? What can I do to escape it?
Update:
json_encode was mangling the HTML (the description field). It produced a br tag that was invalid <br />, notice the forward slash. I assume this is limited to 5.2.8. To correct this I just stripped the br tags as a temporary solution until I can convince the host to upgrade.
preg_replace('/\<br\s\/>/', '', string);
http://codepad.org/oWMMVwsE
PHP will encode the utf8 character.
http://jsfiddle.net/
jQuery can parse it fine.
If your echoing it to the browser, dont forget the browser will interpret the utf8 encoding and display the character. so the json encoded string must be read from CLI or from the source.