Reputation: 33
i have some issue with a json stored in mysql TEXT field, the thing is that everything is OK until i use ' (single quote) the json then is saved \' and every time i save it back it appends a slash like: \' \\' \\' etc.
then some characters are not encoded correctly \u00e0
<div class="wpbs-calendar-legend" data-info="{"default":{"name":{"default":"Available","hr":"Slobodno","cs":"Volno","da":"Ledigt","nl":"Vrij","en":"Available","fr":"Libre","de":"Frei","hu":"Szabad","it":"Libero","ro":"Disponobil","ru":"\u0414\u043e\u0441\u0442\u0443\u043f\u043d\u043e","sk":"Vo\u013en\u00fd","es":"Libre","sv":"Ledigt","uk":"B\u0456\u043b\u044c\u043d\u043e","no":""},"color":"#f1ffcc","splitColor":false,"bookable":"yes","auto-pending":"no","sync":"no"},"1":{"name":{"default":"Booked","hr":"Zauzeto","cs":"Obsazeno","da":"Booket","nl":"Bezet","en":"Booked","fr":"Occup\u00e9","de":"Belegt","hu":"Foglalt","it":"Prenotato","ro":"Rezervat","ru":"\u0417\u0430\u043d\u044f\u0442\u043e","sk":"Obsaden\u00fd","es":"Reservado","sv":"Bokat","uk":"\u0417\u0430\u0439\u043d\u044f\u0442\u043e","no":""},"color":"#ff524c","splitColor":false,"bookable":false,"auto-pending":"yes","sync":"yes"},"2":{"name":{"en":">14H","nl":"","et":"","fi":"","fr":"D\u00e8s 14H","de":"","pt":"","ro":"","default":"D\u00e8s 14H"},"color":"#f1ffcc","splitColor":"#ff524c","bookable":"yes","hide":false,"auto-pending":"no","sync":"no"},"3":{"name":{"en":"<12H","nl":"","et":"","fi":"","fr":"Jusqu\\" \u00e0="" 10h","de":"","pt":"","ro":"","default":"jusqu\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\u00e0="" 10h"},"color":"#ff524c","splitcolor":"#f1ffcc","bookable":"yes","hide":false,"auto-pending":"no","sync":"no"}}'=""></div>
and in my insepct window looks like this:
I am using WordPress so this json_encoded string is saved using update_option into the database. I've tried to stripslashes() the json encoded string on update_option but that did not solve the issue.
may somebody can help me out here?
Thanks for your time on this!
Upvotes: 1
Views: 628
Reputation: 142298
No, not htmlentities, not stripslashes. If you got the JSON from json_encode()
, use an extra parameter to avoid the "Unicode" encoding:
$x = json_encode($s, JSON_UNESCAPED_UNICODE);
See Example #2 in http://php.net/manual/en/function.json-encode.php (Requires PHP 5.4.0)
Upvotes: 1
Reputation: 1390
It looks like your SQL insert is escaping - very correctly - the data.
Now when you need to get the data from SQL you need to remove escaping by the use of stripslashes.
So if your variable is $jsonResult
use it as stripslashes($jsonResult)
.
Upvotes: 1