Answer the question
In order to leave comments, you need to log in
How to solve the problem of stripping slashes in json_encode?
There is an array like
array(
"name"=>"Сулаймон",
"custom"=> array(
array(
"name":"country",
"content":"Узбекистан"
),
array(
"name":"last_date",
"content":"2015-09-15"
)
)
)
{"name":"u043cu0438u0445u0430u0438u043b","custom":[{"name":"country","content":"u0422u0438u0440u0430u0441u043fu043eu043bu044c"},{"name":"last_date","content":"2015-09-15"}]}
Answer the question
In order to leave comments, you need to log in
In general, this is how:
1. If there is a problem with encoding: the second parameter of the json_encode() function is the JSON_UNESCAPED_UNICODE flag. Then non-ANSI characters are not translated into codes. It is only necessary to ensure that all work with data is done in UTF-8.
2. There is a similar problem when working with PDO: if you use prepared queries and form a query with already inserted data, and not through bind(), then PDO filters the query text and removes slashes. If done as it should be for prepared queries - via bind() - then the slashes remain as they are.
I bet you have old PHP/libjson/etc.
Kubuntu 14.04
PHP 5.6.12-1+deb.sury.org~trusty+1 (cli)
$x = array(
"name" => "Сулаймон",
"custom" => array(
array(
"name" => "country",
"content" => "Узбекистан",
),
array(
"name" => "last_date",
"content" => "2015-09-15",
),
),
);
echo json_encode($x);
// {"name":"\u0421\u0443\u043b\u0430\u0439\u043c\u043e\u043d","custom":[{"name":"country","content":"\u0423\u0437\u0431\u0435\u043a\u0438\u0441\u0442\u0430\u043d"},{"name":"last_date","content":"2015-09-15"}]}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question