Reputation: 67
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);
Upvotes: 0
Views: 1343
Reputation: 944568
Your output has a trailing comma after the last property value:
},
}
This is invalid JSON. It looks like your copy of PHP has a severely broken implementation of json_encode
.
I'd look at trying your code on a clean virtual machine with a fresh install of PHP.
Upvotes: 1
Reputation: 10613
PHP will encode the utf8 character.
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.
Upvotes: 1