note 107250 modified in function.json-encode by danbrown

From: Date: Thu, 10 May 2012 21:35:03 +0000
Subject: note 107250 modified in function.json-encode by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187910@lists.php.net to get a copy of this message
Guys, (and girls)&#13;&#10;&#13;&#10;A trick to unescape UTF8 for ppl with php &#60; 5.4.0&#13;&#10;&#13;&#10;json_encode(...) gives you \\u..... right ?&#13;&#10;&#13;&#10;json_decode DOES unescape though !&#13;&#10;&#13;&#10;so:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;/* Imagine you have an object like this :&#13;&#10;[{&#34;name&#34;:&#34;php help héhéhahéhé&#34;,&#34;url&#34;:&#34;http://payAttention.example.com&#34;},{&#34;name&#34;:&#34;took me brainack j&#39;étais mal mec&#34;,&#34;url&#34;:&#34;http://slashesEscapingSux&#34;}]&#13;&#10;*/&#13;&#10;&#13;&#10;//first encode your object&#13;&#10;$myDirtyString = $json_encode($myObject);&#13;&#10;&#13;&#10;/*the JSON_UNESCAPED_SLASHES, and JSON_UNESCAPED_UNICODE being unavailable, you&#39;ll have some ugly escaping happening :&#13;&#10;&#13;&#10;[{&#34;name&#34;:&#34;php help h\\u00e9h\\u00e9hah\\u00e9h\\u00e9&#34;,&#34;url&#34;:&#34;http:\\/\\/payAttention.example.com&#34;},{&#34;name&#34;:&#34;took me brainack j&#39;\\u00e9tais mal mec&#34;,&#34;url&#34;:&#34;http:\\/\\/slashesEscapingSux\\/&#34;}]&#13;&#10;*/&#13;&#10;&#13;&#10;//So, you&#39;ll have to unescape slashes:&#13;&#10;$myDirtyString = str_replace(&#34;\\/&#34;,&#34;/&#34;,$myDirtyString);&#13;&#10;&#13;&#10;//Then, for the trick, escape doule quotes&#13;&#10;$myDirtyString = str_replace(&#39;&#34;&#39;,&#39;\\\\&#34;&#39;,$myDirtyString);&#13;&#10;&#13;&#10;//in oder to json_decode this trciked string (and get your utf8 unescaped)&#13;&#10;&#13;&#10;$myCleanedString = json_decode(&#39;&#34;&#39;.$myDirtyString.&#39;&#34;&#39;);&#13;&#10;&#13;&#10;// Je tour est joué !&#13;&#10;// There might be better ways to do it but i found so much useless nonsense on forums that i decided to go with it for tonight&#13;&#10;?&#62; --was-- Guys, (and girls) A trick to unescape UTF8 for ppl with php < 5.4.0 json_encode(...) gives you \u..... right ? json_decode DOES unescape though ! so: /* Imagine you have an object like this : [{"name":"php help héhéhahéhé","url":"http://payAttention.com"},{"name":"took me brainack j'étais mal mec","url":"http://slashesEscapingSux"}] */ //first encode your object $myDirtyString = $json_encode($myObject); /*the JSON_UNESCAPED_SLASHES, and JSON_UNESCAPED_UNICODE being unavailable, you'll have some ugly escaping happening : [{"name":"php help h\u00e9h\u00e9hah\u00e9h\u00e9","url":"http:\/\/payAttention.com"},{"name":"took me brainack j'\u00e9tais mal mec","url":"http:\/\/slashesEscapingSux\/"}] */ //So, you'll have to unescape slashes: $myDirtyString = str_replace("\/","/",$myDirtyString); //Then, for the trick, escape doule quotes $myDirtyString = str_replace('"','\\"',$myDirtyString); //in oder to json_decode this trciked string (and get your utf8 unescaped) $myCleanedString = json_decode('"'.$myDirtyString.'"'); // Je tour est joué ! // There might be better ways to do it but i found so much useless nonsense on forums that i decided to go with it for tonight http://php.net/manual/en/function.json-encode.php

« previous php.notes (#187910) next »