note 78719 added to function.json-encode

From: Date: Wed, 24 Oct 2007 19:07:31 +0000
Subject: note 78719 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-131699@lists.php.net to get a copy of this message
Another way to work with Russian characters. This procedure just handles Cyrillic characters without UTF conversion. Thanks to JsHttpRequest developers. <?php function php2js($a=false) { if (is_null($a)) return 'null'; if ($a === false) return 'false'; if ($a === true) return 'true'; if (is_scalar($a)) { if (is_float($a)) { // Always use "." for floats. $a = str_replace(",", ".", strval($a)); } // All scalars are converted to strings to avoid indeterminism. // PHP's "1" and 1 are equal for all PHP operators, but // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend, // we should get the same result in the JS frontend (string). // Character replacements for JSON. static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"')); return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"'; } $isList = true; for ($i = 0, reset($a); $i < count($a); $i++, next($a)) { if (key($a) !== $i) { $isList = false; break; } } $result = array(); if ($isList) { foreach ($a as $v) $result[] = php2js($v); return '[ ' . join(', ', $result) . ' ]'; } else { foreach ($a as $k => $v) $result[] = php2js($k).': '.php2js($v); return '{ ' . join(', ', $result) . ' }'; } } ?> ---- Server IP: 195.54.192.44 Probable Submitter: 85.30.223.133 ---- Manual Page -- http://www.php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/78719 Del: integrated -- https://master.php.net/note/delete/78719/integrated Del: useless -- https://master.php.net/note/delete/78719/useless Del: bad code -- https://master.php.net/note/delete/78719/bad+code Del: spam -- https://master.php.net/note/delete/78719/spam Del: non-english -- https://master.php.net/note/delete/78719/non-english Del: in docs -- https://master.php.net/note/delete/78719/in+docs Del: other reasons-- https://master.php.net/note/delete/78719 Reject -- https://master.php.net/note/reject/78719 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131699) next »