note 73706 added to function.json-encode

From: Date: Wed, 07 Mar 2007 12:08:46 +0000
Subject: note 73706 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-124191@lists.php.net to get a copy of this message
This is an update to my previous post. The previous one did not handle null characters (and other characters from 00..1F range) properly. This does. function myjson($s) { if(is_numeric($s)) return $s; if(is_string($s)) return preg_replace("@([\1-\037])@e", "sprintf('\\\\u%04X',ord('$1'))", str_replace("\0", '\u0000', utf8_decode(json_encode(utf8_encode($s))))); if($s===false) return 'false'; if($s===true) return 'true'; if(is_array($s)) { $c=0; foreach($s as $k=>&$v) if($k !== $c++) { foreach($s as $k=>&$v) $v = myjson((string)$k).':'.myjson($v); return '{'.join(',', $s).'}'; } return '[' . join(',', array_map('myjson', $s)) . ']'; } return 'null'; } (Now I just hope the comment posting form doesn't do anything funny to backslashes and quotes.) ---- Server IP: 80.81.171.181 Probable Submitter: 194.157.58.35 ---- Manual Page -- http://www.php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/73706 Del: integrated -- https://master.php.net/note/delete/73706/integrated Del: useless -- https://master.php.net/note/delete/73706/useless Del: bad code -- https://master.php.net/note/delete/73706/bad+code Del: spam -- https://master.php.net/note/delete/73706/spam Del: non-english -- https://master.php.net/note/delete/73706/non-english Del: in docs -- https://master.php.net/note/delete/73706/in+docs Del: other reasons-- https://master.php.net/note/delete/73706 Reject -- https://master.php.net/note/reject/73706 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124191) next »