json_encode function: special characters - php

Elements of an array containing special characters are converted to empty strings when encoding the array with json_encode:
$arr = array ( "funds" => "ComStage STOXX®Europe 600 Techn NR ETF", "time"=>....);
$json = json_encode($arr);
After JSON encoding the element [funds] is null. It happens only with special characters (copyright, trademark etc) like the ones in "ComStage STOXX®Europe 600 Techn NR ETF".
Any suggestions?
Thanks
UPDATE: This is what solved the problem prior to populating the array (all names are taken from the db):
$mysqli->query("SET NAMES 'utf8'");

The manual for json_encode specifies this:
All string data must be UTF-8 encoded.
Thus, try array_mapping utf8_encode() to your array before you encode it:
$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);
// {"funds":"ComStage STOXX\u00c2\u00aeEurope 600 Techn NR ETF"}
For reference, take a look at the differences between the three examples on this fiddle. The first doesn't use character encoding, the second uses htmlentities and the third uses utf8_encode - they all return different results.
For consistency, you should use utf8_encode().
Docs
json_encode()
utf8_encode()
array_map()

Your input has to be encoded as UTF-8 or ISO-8859-1.
http://www.php.net/manual/en/function.json-encode.php
Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.
Since 5.5.0 The return value on failure was changed from null string to FALSE.

To me, it works this way:
# Creating the ARRAY from Result.
$array=array();
while($row = $result->fetch_array(MYSQL_ASSOC))
{
# Converting each column to UTF8
$row = array_map('utf8_encode', $row);
array_push($array,$row);
}
json_encode($array);

you should use this code:
$json = json_encode(array_map('utf8_encode', $arr))
array_map function converts special characters in UTF8 standard

Use the below function.
function utf8_converter($array)
{
array_walk_recursive($array, function (&$item, $key) {
if (!mb_detect_encoding($item, 'utf-8', true)) {
$item = utf8_encode($item);
}
});
return $array;
}

To avoid escaping of special characters, I'm passing the flag:
JSON_UNESCAPED_UNICODE
Like this:
json_encode($array, JSON_UNESCAPED_UNICODE)

Use this code
mysql_set_charset("UTF8", $connection);
Example
$connection = mysql_connect(DB_HOST_GATEWAY,DB_USER_GATEWAY, DB_PASSWORD_GATEWAY);
mysql_set_charset("UTF8", $connection);
mysql_select_db(DB_DATABASE_GATEWAY,$connection);

you should add charset=UTF-8 in meta tag and use json_encode for special characters
$json = json_encode($arr);
json_encode function converts special characters in UTF8 standard

To fix the special character issue you just have to do 2 things
1.mysql_set_charset('utf8'); // set this line on top of your page in which you are using json.
If you are saving json data in database make sure that the particular column collation is set to "latin1_swedish_ci".

Related

How to return {"a": "\u0002"} to browser in php?

I want to send some data to browser, I write code like below.
<?php
$arr = array('a'=> "\u0002 hello");
exit(json_encode($arr));
?>
and I get a result:
but I wanna a result like below, \u0002 not \\u0002, what should I do?
"\u0002" in PHP is not the character with code 2 (why do you use it?) but a string that contains \, u and the four digits.
Use the chr() function to produce a character from its numeric code.
$arr = array('a'=> chr(2)." hello");
echo(json_encode($arr));
Check it online.
\u#### is JSON's unicode escape format, and has no meaning in a PHP string.
Using PHP's actual unicode escape:
$arr = array('a'=> "\u{0002} hello");
Or, since codepoints below 128 / 0x80 have single-byte representations, you can get away with:
$arr = array('a'=> "\x02 hello");
Both will produce the desired output: {"a":"\u0002 hello"}

Decode string php

I am trying to decode a string in PHP but I have not idea what the encoding is.
My string is
\x5B\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20d\x27Asni\x5Cu00e8res\x20sur\x20Seine\x20Belfort\x22,\x22id\x22\x3A\x225337\x22,\x22lati\x22\x3A\x2248.907864\x22,\x22long\x22\x3A\x222.268167\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20d\x27Asni\x5Cu00e8res\x20sur\x20Seine\x20Les\x20Hauts\x20d\x27Asni\x5Cu00e8res\x22,\x22id\x22\x3A\x225338\x22,\x22lati\x22\x3A\x2248.925983\x22,\x22long\x22\x3A\x222.274256\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Bagneux\x20Port\x20Galand\x22,\x22id\x22\x3A\x225339\x22,\x22lati\x22\x3A\x2248.788372\x22,\x22long\x22\x3A\x222.317539\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Bois\x20Colombes\x20Les\x20Bruy\x5Cu00e8res\x22,\x22id\x22\x3A\x225340\x22,\x22lati\x22\x3A\x2248.908581\x22,\x22long\x22\x3A\x222.262088\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Colombes\x20Foss\x5Cu00e9\x20Jean\x22,\x22id\x22\x3A\x225342\x22,\x22lati\x22\x3A\x2248.93106\x22,\x22long\x22\x3A\x222.267059\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Colombes\x20Wiener\x22,\x22id\x22\x3A\x225343\x22,\x22lati\x22\x3A\x2248.915141\x22,\x22long\x22\x3A\x222.246733\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Courbevoie\x20\x2D\x20Quartier\x20de\x20B\x5Cu00e9con\x22,\x22id\x22\x3A\x225344\x22,\x22lati\x22\x3A\x2248.901328\x22,\x22long\x22\x3A\x222.265769\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Courbevoie\x20\x2D\x20Quartier\x20Faubourg\x20de\x20l\x27Arche\x22,\x22id\x22\x3A\x225345\x22,\x22lati\x22\x3A\x2248.897737\x22,\x22long\x22\x3A\x222.238265\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20Meudon\x20la\x20For\x5Cu00eat\x20Place\x20Centrale\x22,\x22id\x22\x3A\x225346\x22,\x22lati\x22\x3A\x2248.787749\x22,\x22long\x22\x3A\x222.227974\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2010\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224463\x22,\x22lati\x22\x3A\x2248.871982\x22,\x22long\x22\x3A\x222.357889\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2012\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224464\x22,\x22lati\x22\x3A\x2248.840246\x22,\x22long\x22\x3A\x222.388204\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2014\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224465\x22,\x22lati\x22\x3A\x2248.832864\x22,\x22long\x22\x3A\x222.326462\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2015\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224475\x22,\x22lati\x22\x3A\x2248.841647\x22,\x22long\x22\x3A\x222.299668\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2016\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224466\x22,\x22lati\x22\x3A\x2248.863973\x22,\x22long\x22\x3A\x222.277212\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2017\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224467\x22,\x22lati\x22\x3A\x2248.883034\x22,\x22long\x22\x3A\x222.32408\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2018\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224468\x22,\x22lati\x22\x3A\x2248.892666\x22,\x22long\x22\x3A\x222.337346\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2019\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224461\x22,\x22lati\x22\x3A\x2248.876334\x22,\x22long\x22\x3A\x222.39318\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2020\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224469\x22,\x22lati\x22\x3A\x2248.864976\x22,\x22long\x22\x3A\x222.39871\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x205\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224471\x22,\x22lati\x22\x3A\x2248.846319\x22,\x22long\x22\x3A\x222.344516\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x207\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224472\x22,\x22lati\x22\x3A\x2248.856888\x22,\x22long\x22\x3A\x222.319964\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x208\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224473\x22,\x22lati\x22\x3A\x2248.877862\x22,\x22long\x22\x3A\x222.317804\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x209\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224474\x22,\x22lati\x22\x3A\x2248.872497\x22,\x22long\x22\x3A\x222.340366\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20d\x27ALFORTVILLE\x22,\x22id\x22\x3A\x225392\x22,\x22lati\x22\x3A\x2248.80529\x22,\x22long\x22\x3A\x222.420021\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20d\x27ARCUEIL\x22,\x22id\x22\x3A\x225032\x22,\x22lati\x22\x3A\x2248.805973\x22,\x22long\x22\x3A\x222.336826\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20ASNIERES\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224799\x22,\x22lati\x22\x3A\x2248.910354\x22,\x22long\x22\x3A\x222.289417\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20AUBERVILLIERS\x22,\x22id\x22\x3A\x224825\x22,\x22lati\x22\x3A\x2248.914652\x22,\x22long\x22\x3A\x222.381673\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BAGNEUX\x22,\x22id\x22\x3A\x224800\x22,\x22lati\x22\x3A\x2248.798652\x22,\x22long\x22\x3A\x222.304296\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BAGNOLET\x22,\x22id\x22\x3A\x224827\x22,\x22lati\x22\x3A\x2248.868921\x22,\x22long\x22\x3A\x222.417979\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BOIS\x20COLOMBES\x22,\x22id\x22\x3A\x224824\x22,\x22lati\x22\x3A\x2248.914476\x22,\x22long\x22\x3A\x222.267797\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BOULOGNE\x20BILLANCOURT\x22,\x22id\x22\x3A\x224801\x22,\x22lati\x22\x3A\x2248.835388\x22,\x22long\x22\x3A\x222.24031\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CACHAN\x22,\x22id\x22\x3A\x224859\x22,\x22lati\x22\x3A\x2248.794443\x22,\x22long\x22\x3A\x222.331244\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHARENTON\x20LE\x20PONT\x22,\x22id\x22\x3A\x225395\x22,\x22lati\x22\x3A\x2248.819851\x22,\x22long\x22\x3A\x222.415667\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATENAY\x20MALABRY\x22,\x22id\x22\x3A\x224802\x22,\x22lati\x22\x3A\x2248.767087\x22,\x22long\x22\x3A\x222.277421\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATILLON\x22,\x22id\x22\x3A\x225382\x22,\x22lati\x22\x3A\x2248.799868\x22,\x22long\x22\x3A\x222.289823\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATOU\x22,\x22id\x22\x3A\x224531\x22,\x22lati\x22\x3A\x2248.890074\x22,\x22long\x22\x3A\x222.157537\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHOISY\x20LE\x20ROI\x22,\x22id\x22\x3A\x224861\x22,\x22lati\x22\x3A\x2248.762448\x22,\x22long\x22\x3A\x222.406938\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CLAMART\x22,\x22id\x22\x3A\x224803\x22,\x22lati\x22\x3A\x2248.800313\x22,\x22long\x22\x3A\x222.263162\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CLICHY\x22,\x22id\x22\x3A\x224804\x22,\x22lati\x22\x3A\x2248.902389\x22,\x22long\x22\x3A\x222.304312\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20COLOMBES\x22,\x22id\x22\x3A\x224805\x22,\x22lati\x22\x3A\x2248.92276\x22,\x22long\x22\x3A\x222.254343\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20COURBEVOIE\x22,\x22id\x22\x3A\x224806\x22,\x22lati\x22\x3A\x2248.895463\x22,\x22long\x22\x3A\x222.2565\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20DRANCY\x22,\x22id\x22\x3A\x224831\x22,\x22lati\x22\x3A\x2248.925691\x22,\x22long\x22\x3A\x222.389513\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20FRESNES\x22,\x22id\x22\x3A\x225398\x22,\x22lati\x22\x3A\x2248.75534\x22,\x22long\x22\x3A\x222.322471\x22\x7D,\x7B\x22name\x22\x3A\x22mairie\x20de\x20Garches\x22,\x22id\x22\x3A\x225287\x22,\x22lati\x22\x3A\x2248.843147\x22,\x22long\x22\x3A\x222.186586\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20GENNEVILLIERS\x22,\x22id\x22\x3A\x224807\x22,\x22lati\x22\x3A\x2248.925846\x22,\x22long\x22\x3A\x222.294367\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20HOUILLES\x22,\x22id\x22\x3A\x224535\x22,\x22lati\x22\x3A\x2248.923084\x22,\x22long\x22\x3A\x222.186666\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20ISSY\x20LES\x20MOULINEAUX\x22,\x22id\x22\x3A\x224808\x22,\x22lati\x22\x3A\x2248.82423\x22,\x22long\x22\x3A\x222.273643\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20IVRY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224864\x22,\x22lati\x22\x3A\x2248.807809\x22,\x22long\x22\x3A\x222.374647\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20L\x27HAY\x20LES\x20ROSES\x22,\x22id\x22\x3A\x224868\x22,\x22lati\x22\x3A\x2248.778867\x22,\x22long\x22\x3A\x222.337234\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LA\x20COURNEUVE\x22,\x22id\x22\x3A\x224835\x22,\x22lati\x22\x3A\x2248.926341\x22,\x22long\x22\x3A\x222.391157\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LA\x20GARENNE\x20COLOMBES\x22,\x22id\x22\x3A\x224809\x22,\x22lati\x22\x3A\x2248.906762\x22,\x22long\x22\x3A\x222.246125\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20BOURGET\x22,\x22id\x22\x3A\x224837\x22,\x22lati\x22\x3A\x2248.934871\x22,\x22long\x22\x3A\x222.425765\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20PLESSIS\x20ROBINSON\x22,\x22id\x22\x3A\x224823\x22,\x22lati\x22\x3A\x2248.78248\x22,\x22long\x22\x3A\x222.262036\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20PRE\x20SAINT\x20GERVAIS\x22,\x22id\x22\x3A\x224838\x22,\x22lati\x22\x3A\x2248.883069\x22,\x22long\x22\x3A\x222.403281\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LES\x20LILAS\x22,\x22id\x22\x3A\x224840\x22,\x22lati\x22\x3A\x2248.880519\x22,\x22long\x22\x3A\x222.41849\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LEVALLOIS\x22,\x22id\x22\x3A\x224810\x22,\x22lati\x22\x3A\x2248.893086\x22,\x22long\x22\x3A\x222.288514\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MALAKOFF\x22,\x22id\x22\x3A\x224811\x22,\x22lati\x22\x3A\x2248.820888\x22,\x22long\x22\x3A\x222.301668\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MEUDON\x22,\x22id\x22\x3A\x224812\x22,\x22lati\x22\x3A\x2248.812558\x22,\x22long\x22\x3A\x222.238593\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MONTROUGE\x22,\x22id\x22\x3A\x224813\x22,\x22lati\x22\x3A\x2248.818705\x22,\x22long\x22\x3A\x222.319896\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20NANTERRE\x22,\x22id\x22\x3A\x224814\x22,\x22lati\x22\x3A\x2248.892044\x22,\x22long\x22\x3A\x222.205266\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20NEUILLY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224815\x22,\x22lati\x22\x3A\x2248.885111\x22,\x22long\x22\x3A\x222.266186\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20PANTIN\x22,\x22id\x22\x3A\x224845\x22,\x22lati\x22\x3A\x2248.896479\x22,\x22long\x22\x3A\x222.401907\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20PUTEAUX\x22,\x22id\x22\x3A\x224816\x22,\x22lati\x22\x3A\x2248.8843\x22,\x22long\x22\x3A\x222.236835\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20RUEIL\x20MALMAISON\x22,\x22id\x22\x3A\x224817\x22,\x22lati\x22\x3A\x2248.877939\x22,\x22long\x22\x3A\x222.180679\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20CLOUD\x22,\x22id\x22\x3A\x224818\x22,\x22lati\x22\x3A\x2248.843706\x22,\x22long\x22\x3A\x222.219356\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x2DDENIS\x22,\x22id\x22\x3A\x224851\x22,\x22lati\x22\x3A\x2248.936005\x22,\x22long\x22\x3A\x222.358906\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20MANDE\x22,\x22id\x22\x3A\x225407\x22,\x22lati\x22\x3A\x2248.843501\x22,\x22long\x22\x3A\x222.419041\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20MAURICE\x22,\x22id\x22\x3A\x224873\x22,\x22lati\x22\x3A\x2248.818091\x22,\x22long\x22\x3A\x222.423183\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20OUEN\x22,\x22id\x22\x3A\x224850\x22,\x22lati\x22\x3A\x2248.912188\x22,\x22long\x22\x3A\x222.333285\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SCEAUX\x22,\x22id\x22\x3A\x224819\x22,\x22lati\x22\x3A\x2248.778775\x22,\x22long\x22\x3A\x222.288772\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SEVRES\x22,\x22id\x22\x3A\x224820\x22,\x22lati\x22\x3A\x2248.824434\x22,\x22long\x22\x3A\x222.21308\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SURESNES\x22,\x22id\x22\x3A\x224821\x22,\x22lati\x22\x3A\x2248.87113\x22,\x22long\x22\x3A\x222.224667\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20THIAIS\x22,\x22id\x22\x3A\x224875\x22,\x22lati\x22\x3A\x2248.765315\x22,\x22long\x22\x3A\x222.388829\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VANVES\x22,\x22id\x22\x3A\x224822\x22,\x22lati\x22\x3A\x2248.821396\x22,\x22long\x22\x3A\x222.289619\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VELIZY\x20VILLACOUBLAY\x22,\x22id\x22\x3A\x224558\x22,\x22lati\x22\x3A\x2248.782531\x22,\x22long\x22\x3A\x222.191265\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VILLEJUIF\x22,\x22id\x22\x3A\x224876\x22,\x22lati\x22\x3A\x2248.794931\x22,\x22long\x22\x3A\x222.366248\x22\x7D,\x7B\x22name\x22\x3A\x22mairie\x20de\x20Villeneuve\x2Dla\x2DGarenne\x22,\x22id\x22\x3A\x223990\x22,\x22lati\x22\x3A\x2248.935596\x22,\x22long\x22\x3A\x222.332934\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VIROFLAY\x22,\x22id\x22\x3A\x224559\x22,\x22lati\x22\x3A\x2248.799422\x22,\x22long\x22\x3A\x222.172793\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VITRY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224878\x22,\x22lati\x22\x3A\x2248.788757\x22,\x22long\x22\x3A\x222.38929\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20LE\x20KREMLIN\x20BICETRE\x22,\x22id\x22\x3A\x225400\x22,\x22lati\x22\x3A\x2248.812652\x22,\x22long\x22\x3A\x222.356646\x22\x7D,\x7B\x22name\x22\x3A\x22Pr\x5Cu00e9fecture\x20de\x20Police\x20\x20\x2D\x20Site\x20de\x20Gesvres\x204\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224470\x22,\x22lati\x22\x3A\x2248.856894\x22,\x22long\x22\x3A\x222.348783\x22\x7D\x5D
I tried json_decode($myString) but it doesn't works
it works in javascript JSON.parse(...)
This is a JSON alright but in hex so converting from hex to string and should output to browser as JSON
<?php
header('Content-type: application/json');
function decode_code($code){
return preg_replace_callback(
"#\\\(x)?([0-9a-f]{2,3})#",
function($m){
return chr($m[1]?hexdec($m[2]):octdec($m[2]));
},
$code
);
}
echo (decode_code("\x5B\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20d\x27Asni\x5Cu00e8res\x20sur\x20Seine\x20Belfort\x22,\x22id\x22\x3A\x225337\x22,\x22lati\x22\x3A\x2248.907864\x22,\x22long\x22\x3A\x222.268167\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20d\x27Asni\x5Cu00e8res\x20sur\x20Seine\x20Les\x20Hauts\x20d\x27Asni\x5Cu00e8res\x22,\x22id\x22\x3A\x225338\x22,\x22lati\x22\x3A\x2248.925983\x22,\x22long\x22\x3A\x222.274256\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Bagneux\x20Port\x20Galand\x22,\x22id\x22\x3A\x225339\x22,\x22lati\x22\x3A\x2248.788372\x22,\x22long\x22\x3A\x222.317539\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Bois\x20Colombes\x20Les\x20Bruy\x5Cu00e8res\x22,\x22id\x22\x3A\x225340\x22,\x22lati\x22\x3A\x2248.908581\x22,\x22long\x22\x3A\x222.262088\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Colombes\x20Foss\x5Cu00e9\x20Jean\x22,\x22id\x22\x3A\x225342\x22,\x22lati\x22\x3A\x2248.93106\x22,\x22long\x22\x3A\x222.267059\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Colombes\x20Wiener\x22,\x22id\x22\x3A\x225343\x22,\x22lati\x22\x3A\x2248.915141\x22,\x22long\x22\x3A\x222.246733\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Courbevoie\x20\x2D\x20Quartier\x20de\x20B\x5Cu00e9con\x22,\x22id\x22\x3A\x225344\x22,\x22lati\x22\x3A\x2248.901328\x22,\x22long\x22\x3A\x222.265769\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Courbevoie\x20\x2D\x20Quartier\x20Faubourg\x20de\x20l\x27Arche\x22,\x22id\x22\x3A\x225345\x22,\x22lati\x22\x3A\x2248.897737\x22,\x22long\x22\x3A\x222.238265\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20Meudon\x20la\x20For\x5Cu00eat\x20Place\x20Centrale\x22,\x22id\x22\x3A\x225346\x22,\x22lati\x22\x3A\x2248.787749\x22,\x22long\x22\x3A\x222.227974\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2010\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224463\x22,\x22lati\x22\x3A\x2248.871982\x22,\x22long\x22\x3A\x222.357889\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2012\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224464\x22,\x22lati\x22\x3A\x2248.840246\x22,\x22long\x22\x3A\x222.388204\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2014\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224465\x22,\x22lati\x22\x3A\x2248.832864\x22,\x22long\x22\x3A\x222.326462\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2015\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224475\x22,\x22lati\x22\x3A\x2248.841647\x22,\x22long\x22\x3A\x222.299668\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2016\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224466\x22,\x22lati\x22\x3A\x2248.863973\x22,\x22long\x22\x3A\x222.277212\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2017\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224467\x22,\x22lati\x22\x3A\x2248.883034\x22,\x22long\x22\x3A\x222.32408\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2018\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224468\x22,\x22lati\x22\x3A\x2248.892666\x22,\x22long\x22\x3A\x222.337346\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2019\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224461\x22,\x22lati\x22\x3A\x2248.876334\x22,\x22long\x22\x3A\x222.39318\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2020\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224469\x22,\x22lati\x22\x3A\x2248.864976\x22,\x22long\x22\x3A\x222.39871\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x205\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224471\x22,\x22lati\x22\x3A\x2248.846319\x22,\x22long\x22\x3A\x222.344516\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x207\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224472\x22,\x22lati\x22\x3A\x2248.856888\x22,\x22long\x22\x3A\x222.319964\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x208\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224473\x22,\x22lati\x22\x3A\x2248.877862\x22,\x22long\x22\x3A\x222.317804\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x209\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224474\x22,\x22lati\x22\x3A\x2248.872497\x22,\x22long\x22\x3A\x222.340366\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20d\x27ALFORTVILLE\x22,\x22id\x22\x3A\x225392\x22,\x22lati\x22\x3A\x2248.80529\x22,\x22long\x22\x3A\x222.420021\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20d\x27ARCUEIL\x22,\x22id\x22\x3A\x225032\x22,\x22lati\x22\x3A\x2248.805973\x22,\x22long\x22\x3A\x222.336826\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20ASNIERES\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224799\x22,\x22lati\x22\x3A\x2248.910354\x22,\x22long\x22\x3A\x222.289417\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20AUBERVILLIERS\x22,\x22id\x22\x3A\x224825\x22,\x22lati\x22\x3A\x2248.914652\x22,\x22long\x22\x3A\x222.381673\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BAGNEUX\x22,\x22id\x22\x3A\x224800\x22,\x22lati\x22\x3A\x2248.798652\x22,\x22long\x22\x3A\x222.304296\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BAGNOLET\x22,\x22id\x22\x3A\x224827\x22,\x22lati\x22\x3A\x2248.868921\x22,\x22long\x22\x3A\x222.417979\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BOIS\x20COLOMBES\x22,\x22id\x22\x3A\x224824\x22,\x22lati\x22\x3A\x2248.914476\x22,\x22long\x22\x3A\x222.267797\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BOULOGNE\x20BILLANCOURT\x22,\x22id\x22\x3A\x224801\x22,\x22lati\x22\x3A\x2248.835388\x22,\x22long\x22\x3A\x222.24031\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CACHAN\x22,\x22id\x22\x3A\x224859\x22,\x22lati\x22\x3A\x2248.794443\x22,\x22long\x22\x3A\x222.331244\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHARENTON\x20LE\x20PONT\x22,\x22id\x22\x3A\x225395\x22,\x22lati\x22\x3A\x2248.819851\x22,\x22long\x22\x3A\x222.415667\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATENAY\x20MALABRY\x22,\x22id\x22\x3A\x224802\x22,\x22lati\x22\x3A\x2248.767087\x22,\x22long\x22\x3A\x222.277421\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATILLON\x22,\x22id\x22\x3A\x225382\x22,\x22lati\x22\x3A\x2248.799868\x22,\x22long\x22\x3A\x222.289823\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATOU\x22,\x22id\x22\x3A\x224531\x22,\x22lati\x22\x3A\x2248.890074\x22,\x22long\x22\x3A\x222.157537\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHOISY\x20LE\x20ROI\x22,\x22id\x22\x3A\x224861\x22,\x22lati\x22\x3A\x2248.762448\x22,\x22long\x22\x3A\x222.406938\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CLAMART\x22,\x22id\x22\x3A\x224803\x22,\x22lati\x22\x3A\x2248.800313\x22,\x22long\x22\x3A\x222.263162\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CLICHY\x22,\x22id\x22\x3A\x224804\x22,\x22lati\x22\x3A\x2248.902389\x22,\x22long\x22\x3A\x222.304312\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20COLOMBES\x22,\x22id\x22\x3A\x224805\x22,\x22lati\x22\x3A\x2248.92276\x22,\x22long\x22\x3A\x222.254343\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20COURBEVOIE\x22,\x22id\x22\x3A\x224806\x22,\x22lati\x22\x3A\x2248.895463\x22,\x22long\x22\x3A\x222.2565\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20DRANCY\x22,\x22id\x22\x3A\x224831\x22,\x22lati\x22\x3A\x2248.925691\x22,\x22long\x22\x3A\x222.389513\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20FRESNES\x22,\x22id\x22\x3A\x225398\x22,\x22lati\x22\x3A\x2248.75534\x22,\x22long\x22\x3A\x222.322471\x22\x7D,\x7B\x22name\x22\x3A\x22mairie\x20de\x20Garches\x22,\x22id\x22\x3A\x225287\x22,\x22lati\x22\x3A\x2248.843147\x22,\x22long\x22\x3A\x222.186586\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20GENNEVILLIERS\x22,\x22id\x22\x3A\x224807\x22,\x22lati\x22\x3A\x2248.925846\x22,\x22long\x22\x3A\x222.294367\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20HOUILLES\x22,\x22id\x22\x3A\x224535\x22,\x22lati\x22\x3A\x2248.923084\x22,\x22long\x22\x3A\x222.186666\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20ISSY\x20LES\x20MOULINEAUX\x22,\x22id\x22\x3A\x224808\x22,\x22lati\x22\x3A\x2248.82423\x22,\x22long\x22\x3A\x222.273643\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20IVRY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224864\x22,\x22lati\x22\x3A\x2248.807809\x22,\x22long\x22\x3A\x222.374647\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20L\x27HAY\x20LES\x20ROSES\x22,\x22id\x22\x3A\x224868\x22,\x22lati\x22\x3A\x2248.778867\x22,\x22long\x22\x3A\x222.337234\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LA\x20COURNEUVE\x22,\x22id\x22\x3A\x224835\x22,\x22lati\x22\x3A\x2248.926341\x22,\x22long\x22\x3A\x222.391157\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LA\x20GARENNE\x20COLOMBES\x22,\x22id\x22\x3A\x224809\x22,\x22lati\x22\x3A\x2248.906762\x22,\x22long\x22\x3A\x222.246125\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20BOURGET\x22,\x22id\x22\x3A\x224837\x22,\x22lati\x22\x3A\x2248.934871\x22,\x22long\x22\x3A\x222.425765\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20PLESSIS\x20ROBINSON\x22,\x22id\x22\x3A\x224823\x22,\x22lati\x22\x3A\x2248.78248\x22,\x22long\x22\x3A\x222.262036\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20PRE\x20SAINT\x20GERVAIS\x22,\x22id\x22\x3A\x224838\x22,\x22lati\x22\x3A\x2248.883069\x22,\x22long\x22\x3A\x222.403281\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LES\x20LILAS\x22,\x22id\x22\x3A\x224840\x22,\x22lati\x22\x3A\x2248.880519\x22,\x22long\x22\x3A\x222.41849\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LEVALLOIS\x22,\x22id\x22\x3A\x224810\x22,\x22lati\x22\x3A\x2248.893086\x22,\x22long\x22\x3A\x222.288514\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MALAKOFF\x22,\x22id\x22\x3A\x224811\x22,\x22lati\x22\x3A\x2248.820888\x22,\x22long\x22\x3A\x222.301668\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MEUDON\x22,\x22id\x22\x3A\x224812\x22,\x22lati\x22\x3A\x2248.812558\x22,\x22long\x22\x3A\x222.238593\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MONTROUGE\x22,\x22id\x22\x3A\x224813\x22,\x22lati\x22\x3A\x2248.818705\x22,\x22long\x22\x3A\x222.319896\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20NANTERRE\x22,\x22id\x22\x3A\x224814\x22,\x22lati\x22\x3A\x2248.892044\x22,\x22long\x22\x3A\x222.205266\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20NEUILLY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224815\x22,\x22lati\x22\x3A\x2248.885111\x22,\x22long\x22\x3A\x222.266186\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20PANTIN\x22,\x22id\x22\x3A\x224845\x22,\x22lati\x22\x3A\x2248.896479\x22,\x22long\x22\x3A\x222.401907\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20PUTEAUX\x22,\x22id\x22\x3A\x224816\x22,\x22lati\x22\x3A\x2248.8843\x22,\x22long\x22\x3A\x222.236835\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20RUEIL\x20MALMAISON\x22,\x22id\x22\x3A\x224817\x22,\x22lati\x22\x3A\x2248.877939\x22,\x22long\x22\x3A\x222.180679\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20CLOUD\x22,\x22id\x22\x3A\x224818\x22,\x22lati\x22\x3A\x2248.843706\x22,\x22long\x22\x3A\x222.219356\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x2DDENIS\x22,\x22id\x22\x3A\x224851\x22,\x22lati\x22\x3A\x2248.936005\x22,\x22long\x22\x3A\x222.358906\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20MANDE\x22,\x22id\x22\x3A\x225407\x22,\x22lati\x22\x3A\x2248.843501\x22,\x22long\x22\x3A\x222.419041\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20MAURICE\x22,\x22id\x22\x3A\x224873\x22,\x22lati\x22\x3A\x2248.818091\x22,\x22long\x22\x3A\x222.423183\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20OUEN\x22,\x22id\x22\x3A\x224850\x22,\x22lati\x22\x3A\x2248.912188\x22,\x22long\x22\x3A\x222.333285\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SCEAUX\x22,\x22id\x22\x3A\x224819\x22,\x22lati\x22\x3A\x2248.778775\x22,\x22long\x22\x3A\x222.288772\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SEVRES\x22,\x22id\x22\x3A\x224820\x22,\x22lati\x22\x3A\x2248.824434\x22,\x22long\x22\x3A\x222.21308\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SURESNES\x22,\x22id\x22\x3A\x224821\x22,\x22lati\x22\x3A\x2248.87113\x22,\x22long\x22\x3A\x222.224667\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20THIAIS\x22,\x22id\x22\x3A\x224875\x22,\x22lati\x22\x3A\x2248.765315\x22,\x22long\x22\x3A\x222.388829\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VANVES\x22,\x22id\x22\x3A\x224822\x22,\x22lati\x22\x3A\x2248.821396\x22,\x22long\x22\x3A\x222.289619\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VELIZY\x20VILLACOUBLAY\x22,\x22id\x22\x3A\x224558\x22,\x22lati\x22\x3A\x2248.782531\x22,\x22long\x22\x3A\x222.191265\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VILLEJUIF\x22,\x22id\x22\x3A\x224876\x22,\x22lati\x22\x3A\x2248.794931\x22,\x22long\x22\x3A\x222.366248\x22\x7D,\x7B\x22name\x22\x3A\x22mairie\x20de\x20Villeneuve\x2Dla\x2DGarenne\x22,\x22id\x22\x3A\x223990\x22,\x22lati\x22\x3A\x2248.935596\x22,\x22long\x22\x3A\x222.332934\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VIROFLAY\x22,\x22id\x22\x3A\x224559\x22,\x22lati\x22\x3A\x2248.799422\x22,\x22long\x22\x3A\x222.172793\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VITRY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224878\x22,\x22lati\x22\x3A\x2248.788757\x22,\x22long\x22\x3A\x222.38929\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20LE\x20KREMLIN\x20BICETRE\x22,\x22id\x22\x3A\x225400\x22,\x22lati\x22\x3A\x2248.812652\x22,\x22long\x22\x3A\x222.356646\x22\x7D,\x7B\x22name\x22\x3A\x22Pr\x5Cu00e9fecture\x20de\x20Police\x20\x20\x2D\x20Site\x20de\x20Gesvres\x204\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224470\x22,\x22lati\x22\x3A\x2248.856894\x22,\x22long\x22\x3A\x222.348783\x22\x7D\x5D"));
?>
Your json_encode is encoding unicode characters and they are being escaped. You can change this behaviour by passing the flag JSON_UNESCAPED_UNICODE as second parameter.
echo json_encode($array, JSON_UNESCAPED_UNICODE);
Make sure you're using double quotes:
<?php
$string = "\x5B\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20d\x27Asni\x5Cu00e8res\x20sur\x20Seine\x20Belfort\x22,\x22id\x22\x3A\x225337\x22,\x22lati\x22\x3A\x2248.907864\x22,\x22long\x22\x3A\x222.268167\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20d\x27Asni\x5Cu00e8res\x20sur\x20Seine\x20Les\x20Hauts\x20d\x27Asni\x5Cu00e8res\x22,\x22id\x22\x3A\x225338\x22,\x22lati\x22\x3A\x2248.925983\x22,\x22long\x22\x3A\x222.274256\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Bagneux\x20Port\x20Galand\x22,\x22id\x22\x3A\x225339\x22,\x22lati\x22\x3A\x2248.788372\x22,\x22long\x22\x3A\x222.317539\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Bois\x20Colombes\x20Les\x20Bruy\x5Cu00e8res\x22,\x22id\x22\x3A\x225340\x22,\x22lati\x22\x3A\x2248.908581\x22,\x22long\x22\x3A\x222.262088\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Colombes\x20Foss\x5Cu00e9\x20Jean\x22,\x22id\x22\x3A\x225342\x22,\x22lati\x22\x3A\x2248.93106\x22,\x22long\x22\x3A\x222.267059\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Colombes\x20Wiener\x22,\x22id\x22\x3A\x225343\x22,\x22lati\x22\x3A\x2248.915141\x22,\x22long\x22\x3A\x222.246733\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Courbevoie\x20\x2D\x20Quartier\x20de\x20B\x5Cu00e9con\x22,\x22id\x22\x3A\x225344\x22,\x22lati\x22\x3A\x2248.901328\x22,\x22long\x22\x3A\x222.265769\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20de\x20Courbevoie\x20\x2D\x20Quartier\x20Faubourg\x20de\x20l\x27Arche\x22,\x22id\x22\x3A\x225345\x22,\x22lati\x22\x3A\x2248.897737\x22,\x22long\x22\x3A\x222.238265\x22\x7D,\x7B\x22name\x22\x3A\x22Annexe\x20Mairie\x20Meudon\x20la\x20For\x5Cu00eat\x20Place\x20Centrale\x22,\x22id\x22\x3A\x225346\x22,\x22lati\x22\x3A\x2248.787749\x22,\x22long\x22\x3A\x222.227974\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2010\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224463\x22,\x22lati\x22\x3A\x2248.871982\x22,\x22long\x22\x3A\x222.357889\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2012\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224464\x22,\x22lati\x22\x3A\x2248.840246\x22,\x22long\x22\x3A\x222.388204\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2014\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224465\x22,\x22lati\x22\x3A\x2248.832864\x22,\x22long\x22\x3A\x222.326462\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2015\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224475\x22,\x22lati\x22\x3A\x2248.841647\x22,\x22long\x22\x3A\x222.299668\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2016\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224466\x22,\x22lati\x22\x3A\x2248.863973\x22,\x22long\x22\x3A\x222.277212\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2017\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224467\x22,\x22lati\x22\x3A\x2248.883034\x22,\x22long\x22\x3A\x222.32408\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2018\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224468\x22,\x22lati\x22\x3A\x2248.892666\x22,\x22long\x22\x3A\x222.337346\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2019\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224461\x22,\x22lati\x22\x3A\x2248.876334\x22,\x22long\x22\x3A\x222.39318\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x2020\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224469\x22,\x22lati\x22\x3A\x2248.864976\x22,\x22long\x22\x3A\x222.39871\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x205\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224471\x22,\x22lati\x22\x3A\x2248.846319\x22,\x22long\x22\x3A\x222.344516\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x207\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224472\x22,\x22lati\x22\x3A\x2248.856888\x22,\x22long\x22\x3A\x222.319964\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x208\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224473\x22,\x22lati\x22\x3A\x2248.877862\x22,\x22long\x22\x3A\x222.317804\x22\x7D,\x7B\x22name\x22\x3A\x22Antenne\x20de\x20Police\x209\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224474\x22,\x22lati\x22\x3A\x2248.872497\x22,\x22long\x22\x3A\x222.340366\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20d\x27ALFORTVILLE\x22,\x22id\x22\x3A\x225392\x22,\x22lati\x22\x3A\x2248.80529\x22,\x22long\x22\x3A\x222.420021\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20d\x27ARCUEIL\x22,\x22id\x22\x3A\x225032\x22,\x22lati\x22\x3A\x2248.805973\x22,\x22long\x22\x3A\x222.336826\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20ASNIERES\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224799\x22,\x22lati\x22\x3A\x2248.910354\x22,\x22long\x22\x3A\x222.289417\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20AUBERVILLIERS\x22,\x22id\x22\x3A\x224825\x22,\x22lati\x22\x3A\x2248.914652\x22,\x22long\x22\x3A\x222.381673\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BAGNEUX\x22,\x22id\x22\x3A\x224800\x22,\x22lati\x22\x3A\x2248.798652\x22,\x22long\x22\x3A\x222.304296\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BAGNOLET\x22,\x22id\x22\x3A\x224827\x22,\x22lati\x22\x3A\x2248.868921\x22,\x22long\x22\x3A\x222.417979\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BOIS\x20COLOMBES\x22,\x22id\x22\x3A\x224824\x22,\x22lati\x22\x3A\x2248.914476\x22,\x22long\x22\x3A\x222.267797\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20BOULOGNE\x20BILLANCOURT\x22,\x22id\x22\x3A\x224801\x22,\x22lati\x22\x3A\x2248.835388\x22,\x22long\x22\x3A\x222.24031\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CACHAN\x22,\x22id\x22\x3A\x224859\x22,\x22lati\x22\x3A\x2248.794443\x22,\x22long\x22\x3A\x222.331244\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHARENTON\x20LE\x20PONT\x22,\x22id\x22\x3A\x225395\x22,\x22lati\x22\x3A\x2248.819851\x22,\x22long\x22\x3A\x222.415667\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATENAY\x20MALABRY\x22,\x22id\x22\x3A\x224802\x22,\x22lati\x22\x3A\x2248.767087\x22,\x22long\x22\x3A\x222.277421\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATILLON\x22,\x22id\x22\x3A\x225382\x22,\x22lati\x22\x3A\x2248.799868\x22,\x22long\x22\x3A\x222.289823\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHATOU\x22,\x22id\x22\x3A\x224531\x22,\x22lati\x22\x3A\x2248.890074\x22,\x22long\x22\x3A\x222.157537\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CHOISY\x20LE\x20ROI\x22,\x22id\x22\x3A\x224861\x22,\x22lati\x22\x3A\x2248.762448\x22,\x22long\x22\x3A\x222.406938\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CLAMART\x22,\x22id\x22\x3A\x224803\x22,\x22lati\x22\x3A\x2248.800313\x22,\x22long\x22\x3A\x222.263162\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20CLICHY\x22,\x22id\x22\x3A\x224804\x22,\x22lati\x22\x3A\x2248.902389\x22,\x22long\x22\x3A\x222.304312\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20COLOMBES\x22,\x22id\x22\x3A\x224805\x22,\x22lati\x22\x3A\x2248.92276\x22,\x22long\x22\x3A\x222.254343\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20COURBEVOIE\x22,\x22id\x22\x3A\x224806\x22,\x22lati\x22\x3A\x2248.895463\x22,\x22long\x22\x3A\x222.2565\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20DRANCY\x22,\x22id\x22\x3A\x224831\x22,\x22lati\x22\x3A\x2248.925691\x22,\x22long\x22\x3A\x222.389513\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20FRESNES\x22,\x22id\x22\x3A\x225398\x22,\x22lati\x22\x3A\x2248.75534\x22,\x22long\x22\x3A\x222.322471\x22\x7D,\x7B\x22name\x22\x3A\x22mairie\x20de\x20Garches\x22,\x22id\x22\x3A\x225287\x22,\x22lati\x22\x3A\x2248.843147\x22,\x22long\x22\x3A\x222.186586\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20GENNEVILLIERS\x22,\x22id\x22\x3A\x224807\x22,\x22lati\x22\x3A\x2248.925846\x22,\x22long\x22\x3A\x222.294367\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20HOUILLES\x22,\x22id\x22\x3A\x224535\x22,\x22lati\x22\x3A\x2248.923084\x22,\x22long\x22\x3A\x222.186666\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20ISSY\x20LES\x20MOULINEAUX\x22,\x22id\x22\x3A\x224808\x22,\x22lati\x22\x3A\x2248.82423\x22,\x22long\x22\x3A\x222.273643\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20IVRY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224864\x22,\x22lati\x22\x3A\x2248.807809\x22,\x22long\x22\x3A\x222.374647\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20L\x27HAY\x20LES\x20ROSES\x22,\x22id\x22\x3A\x224868\x22,\x22lati\x22\x3A\x2248.778867\x22,\x22long\x22\x3A\x222.337234\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LA\x20COURNEUVE\x22,\x22id\x22\x3A\x224835\x22,\x22lati\x22\x3A\x2248.926341\x22,\x22long\x22\x3A\x222.391157\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LA\x20GARENNE\x20COLOMBES\x22,\x22id\x22\x3A\x224809\x22,\x22lati\x22\x3A\x2248.906762\x22,\x22long\x22\x3A\x222.246125\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20BOURGET\x22,\x22id\x22\x3A\x224837\x22,\x22lati\x22\x3A\x2248.934871\x22,\x22long\x22\x3A\x222.425765\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20PLESSIS\x20ROBINSON\x22,\x22id\x22\x3A\x224823\x22,\x22lati\x22\x3A\x2248.78248\x22,\x22long\x22\x3A\x222.262036\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LE\x20PRE\x20SAINT\x20GERVAIS\x22,\x22id\x22\x3A\x224838\x22,\x22lati\x22\x3A\x2248.883069\x22,\x22long\x22\x3A\x222.403281\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LES\x20LILAS\x22,\x22id\x22\x3A\x224840\x22,\x22lati\x22\x3A\x2248.880519\x22,\x22long\x22\x3A\x222.41849\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20LEVALLOIS\x22,\x22id\x22\x3A\x224810\x22,\x22lati\x22\x3A\x2248.893086\x22,\x22long\x22\x3A\x222.288514\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MALAKOFF\x22,\x22id\x22\x3A\x224811\x22,\x22lati\x22\x3A\x2248.820888\x22,\x22long\x22\x3A\x222.301668\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MEUDON\x22,\x22id\x22\x3A\x224812\x22,\x22lati\x22\x3A\x2248.812558\x22,\x22long\x22\x3A\x222.238593\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20MONTROUGE\x22,\x22id\x22\x3A\x224813\x22,\x22lati\x22\x3A\x2248.818705\x22,\x22long\x22\x3A\x222.319896\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20NANTERRE\x22,\x22id\x22\x3A\x224814\x22,\x22lati\x22\x3A\x2248.892044\x22,\x22long\x22\x3A\x222.205266\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20NEUILLY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224815\x22,\x22lati\x22\x3A\x2248.885111\x22,\x22long\x22\x3A\x222.266186\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20PANTIN\x22,\x22id\x22\x3A\x224845\x22,\x22lati\x22\x3A\x2248.896479\x22,\x22long\x22\x3A\x222.401907\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20PUTEAUX\x22,\x22id\x22\x3A\x224816\x22,\x22lati\x22\x3A\x2248.8843\x22,\x22long\x22\x3A\x222.236835\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20RUEIL\x20MALMAISON\x22,\x22id\x22\x3A\x224817\x22,\x22lati\x22\x3A\x2248.877939\x22,\x22long\x22\x3A\x222.180679\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20CLOUD\x22,\x22id\x22\x3A\x224818\x22,\x22lati\x22\x3A\x2248.843706\x22,\x22long\x22\x3A\x222.219356\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x2DDENIS\x22,\x22id\x22\x3A\x224851\x22,\x22lati\x22\x3A\x2248.936005\x22,\x22long\x22\x3A\x222.358906\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20MANDE\x22,\x22id\x22\x3A\x225407\x22,\x22lati\x22\x3A\x2248.843501\x22,\x22long\x22\x3A\x222.419041\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20MAURICE\x22,\x22id\x22\x3A\x224873\x22,\x22lati\x22\x3A\x2248.818091\x22,\x22long\x22\x3A\x222.423183\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SAINT\x20OUEN\x22,\x22id\x22\x3A\x224850\x22,\x22lati\x22\x3A\x2248.912188\x22,\x22long\x22\x3A\x222.333285\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SCEAUX\x22,\x22id\x22\x3A\x224819\x22,\x22lati\x22\x3A\x2248.778775\x22,\x22long\x22\x3A\x222.288772\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SEVRES\x22,\x22id\x22\x3A\x224820\x22,\x22lati\x22\x3A\x2248.824434\x22,\x22long\x22\x3A\x222.21308\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20SURESNES\x22,\x22id\x22\x3A\x224821\x22,\x22lati\x22\x3A\x2248.87113\x22,\x22long\x22\x3A\x222.224667\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20THIAIS\x22,\x22id\x22\x3A\x224875\x22,\x22lati\x22\x3A\x2248.765315\x22,\x22long\x22\x3A\x222.388829\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VANVES\x22,\x22id\x22\x3A\x224822\x22,\x22lati\x22\x3A\x2248.821396\x22,\x22long\x22\x3A\x222.289619\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VELIZY\x20VILLACOUBLAY\x22,\x22id\x22\x3A\x224558\x22,\x22lati\x22\x3A\x2248.782531\x22,\x22long\x22\x3A\x222.191265\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VILLEJUIF\x22,\x22id\x22\x3A\x224876\x22,\x22lati\x22\x3A\x2248.794931\x22,\x22long\x22\x3A\x222.366248\x22\x7D,\x7B\x22name\x22\x3A\x22mairie\x20de\x20Villeneuve\x2Dla\x2DGarenne\x22,\x22id\x22\x3A\x223990\x22,\x22lati\x22\x3A\x2248.935596\x22,\x22long\x22\x3A\x222.332934\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VIROFLAY\x22,\x22id\x22\x3A\x224559\x22,\x22lati\x22\x3A\x2248.799422\x22,\x22long\x22\x3A\x222.172793\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20de\x20VITRY\x20SUR\x20SEINE\x22,\x22id\x22\x3A\x224878\x22,\x22lati\x22\x3A\x2248.788757\x22,\x22long\x22\x3A\x222.38929\x22\x7D,\x7B\x22name\x22\x3A\x22Mairie\x20LE\x20KREMLIN\x20BICETRE\x22,\x22id\x22\x3A\x225400\x22,\x22lati\x22\x3A\x2248.812652\x22,\x22long\x22\x3A\x222.356646\x22\x7D,\x7B\x22name\x22\x3A\x22Pr\x5Cu00e9fecture\x20de\x20Police\x20\x20\x2D\x20Site\x20de\x20Gesvres\x204\x5Cu00e8me\x20arrondissement\x22,\x22id\x22\x3A\x224470\x22,\x22lati\x22\x3A\x2248.856894\x22,\x22long\x22\x3A\x222.348783\x22\x7D\x5D";
var_dump(json_decode($string,true));
The above code works OK for me. Good luck!!

How do I convert Unicode escape sequences to text in PHP?

I have this Unicode sequence: \u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059. How do I convert it into text?
$unicode = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';
I tried:
echo $utf8-decode(unicode);
and I tried:
echo mb_convert_encoding($unicode , 'US-ASCII', 'UTF-8');
and I tried:
echo htmlentities($unicode , ENT_COMPAT, "UTF-8");
but none of these functions convert the sequence into the corresponding Japanese text.
The issue here is that the string is not unicode. It is an escape sequence used to note down unicode by means of ASCII characters (so 7bit save).
There is a simply trick to use the phps json decoder for this:
<?php
$sequence = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';
print_r(json_decode('["'.$sequence.'"]'));
The output is:
Array
(
[0] => おはようございます
)
This means you can define a simple convenience function:
<?php
$sequence = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';
function decode($payload) {
return array_pop(json_decode('["'.$payload.'"]'));
}
echo decode($sequence);
You want to add error handling and escaping of json specific control characters inside the payload. This simply example is just meant to point you into the right direction...
Have fun!
Transliterator class from intl extension can handle the convertion with its predefined Hex-Any identifier:
$in = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';
$out = transliterator_create('Hex-Any')->transliterate($in);
var_dump($out); # string(27) "おはようございます"
$unicode = '\u304a\u306f\u3088\u3046\u3054\u3056\u3044\u307e\u3059';
$json = sprintf('"%s"',$unicode); # build json string
$utf8_str = json_decode ( $json, true ); # json decode
echo $utf8_str; # おはようございます
See Json string
PHP 7+
As of PHP 7, you can use the Unicode codepoint escape syntax to do this.
echo "\u{304a}\u{306f}\u{3088}\u{3046}\u{3054}\u{3056}\u{3044}\u{307e}\u{3059}"; outputs おはようございます.

json_encode() non utf-8 strings?

So I have an array of strings, and all of the strings are using the system default ANSI encoding and were pulled from a SQL database. So there are 256 different possible character byte values (single byte encoding).
Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like \u0082?
Or is that the standard for JSON?
Is there a way I can get json_encode() to work and display these characters instead of having to use utf8_encode() on all of my strings and ending up with stuff like "\u0082"?
If you have an ANSI encoded string, using utf8_encode() is the wrong function to deal with this. You need to properly convert it from ANSI to UTF-8 first. That will certainly reduce the number of Unicode escape sequences like \u0082 from the json output, but technically these sequences are valid for json, you must not fear them.
Converting ANSI to UTF-8 with PHP
json_encode works with UTF-8 encoded strings only. If you need to create valid json successfully from an ANSI encoded string, you need to re-encode/convert it to UTF-8 first. Then json_encode will just work as documented.
To convert an encoding from ANSI (more correctly I assume you have a Windows-1252 encoded string, which is popular but wrongly referred to as ANSI) to UTF-8 you can make use of the mb_convert_encoding() function:
$str = mb_convert_encoding($str, "UTF-8", "Windows-1252");
Another function in PHP that can convert the encoding / charset of a string is called iconv based on libiconv. You can use it as well:
$str = iconv("CP1252", "UTF-8", $str);
Note on utf8_encode()
utf8_encode() does only work for Latin-1, not for ANSI. So you will destroy part of your characters inside that string when you run it through that function.
Related: What is ANSI format?
For a more fine-grained control of what json_encode() returns, see the list of predifined constants (PHP version dependent, incl. PHP 5.4, some constants remain undocumented and are available in the source code only so far).
Changing the encoding of an array/iteratively (PDO comment)
As you wrote in a comment that you have problems to apply the function onto an array, here is some code example. It's always needed to first change the encoding before using json_encode. That's just a standard array operation, for the simpler case of pdo::fetch() a foreach iteration:
while($row = $q->fetch(PDO::FETCH_ASSOC))
{
foreach($row as &$value)
{
$value = mb_convert_encoding($value, "UTF-8", "Windows-1252");
}
unset($value); # safety: remove reference
$items[] = array_map('utf8_encode', $row );
}
The JSON standard ENFORCES Unicode encoding. From RFC4627:
3. Encoding
JSON text SHALL be encoded in Unicode. The default encoding is
UTF-8.
Since the first two characters of a JSON text will always be ASCII
characters [RFC0020], it is possible to determine whether an octet
stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking
at the pattern of nulls in the first four octets.
00 00 00 xx UTF-32BE
00 xx 00 xx UTF-16BE
xx 00 00 00 UTF-32LE
xx 00 xx 00 UTF-16LE
xx xx xx xx UTF-8
Therefore, on the strictest sense, ANSI encoded JSON wouldn't be valid JSON; this is why PHP enforces unicode encoding when using json_encode().
As for "default ANSI", I'm pretty sure that your strings are encoded in Windows-1252. It is incorrectly referred to as ANSI.
<?php
$array = array('first word' => array('Слово','Кириллица'),'second word' => 'Кириллица','last word' => 'Кириллица');
echo json_encode($array);
/*
return {"first word":["\u0421\u043b\u043e\u0432\u043e","\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430"],"second word":"\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430","last word":"\u041a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430"}
*/
echo json_encode($array,256);
/*
return {"first word":["Слово","Кириллица"],"second word":"Кириллица","last word":"Кириллица"}
*/
?>
JSON_UNESCAPED_UNICODE (integer)
Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0.
http://php.net/manual/en/json.constants.php#constant.json-unescaped-unicode
I found the following answer for an analogous problem with a nested array not utf-8 encoded that i had to json encode:
$inputArray = array(
'a'=>'First item - à',
'c'=>'Third item - é'
);
$inputArray['b']= array (
'a'=>'First subitem - ù',
'b'=>'Second subitem - ì'
);
if (!function_exists('recursive_utf8')) {
function recursive_utf8 ($data) {
if (!is_array($data)) {
return utf8_encode($data);
}
$result = array();
foreach ($data as $index=>$item) {
if (is_array($item)) {
$result[$index] = array();
foreach($item as $key=>$value) {
$result[$index][$key] = recursive_utf8($value);
}
}
else if (is_object($item)) {
$result[$index] = array();
foreach(get_object_vars($item) as $key=>$value) {
$result[$index][$key] = recursive_utf8($value);
}
}
else {
$result[$index] = recursive_utf8($item);
}
}
return $result;
}
}
$outputArray = json_encode(array_map('recursive_utf8', $inputArray ));
json_encode($str,JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT);
that will convert windows based ANSI to utf-8 and the error will be no more.
Use this instead:
<?php
//$return_arr = the array of data to json encode
//$out = the output of the function
//don't forget to escape the data before use it!
$out = '["' . implode('","', $return_arr) . '"]';
?>
Copy from json_encode php manual's comments. Always read the comments. They are useful.

How you encode strings like \u00d6?

This is encoded: \u00d6
This is decoded: Ö
What function I have to use to decode that string into something readable?
\u00d6asdf -> Öasdf
To convert to UTF-8, do:
preg_replace('/\\\\u([0-9a-f]{4})/ie',
'mb_convert_encoding("&#x$1;", "UTF-8", "HTML-ENTITIES")',
$string);
Since this is the escaping used in JSON, another option would be json_decode. This would, however, also require escaping double quotes and backslashes before (except those of the \uXXXX escape sequences) and adding double quotes around the string. If, however, the string is indeed JSON-encoded and that's what originally motivated the question, the correct answer would naturally be use json_decode instead of the method above.
Normally this would be the urldecode method, but it does not apply to unicode characters, like yours. Try this one instead:
function unicode_urldecode($url)
{
preg_match_all('/%u([[:alnum:]]{4})/', $url, $a);
foreach ($a[1] as $uniord)
{
$utf = '&#x' . $uniord . ';';
$url = str_replace('%u'.$uniord, $utf, $url);
}
return urldecode($url);
}

Categories