@ 2013-07-08T18:49:28Z <?php
if (!function_exists('json_encode')) {
function json_encode($it) {
if (!($assoc = is_object($it)) && !is_array($it)) {
return _js_enc_value($it);
}
$i = 0;
$it = (array)$it;
if (!$assoc) {
foreach ($it as $k => $v) {
if ($k !== $i) {
$assoc = true;
break;
}
$i++;
}
}
$data = array();
foreach ($it as $k => $v) {
$v = _js_enc_value($v);
if ($assoc) {
$k = _js_enc_value($k);
$data[] = $k . ':' . $v;
} else {
$data[] = $v;
}
}
if ($assoc) {
$bl = '{';
$br = '}';
} else {
$bl = '[';
$br = ']';
}
return $bl . implode(',', $data) . $br;
}
function _js_enc_value($value) {
switch (true) {
case is_array($value):
case is_object($value):
return json_encode($value);
case is_bool($value):
return $value ? 'true' : 'false';
case $value === null:
case is_resource($value) && _js_error():
return 'null';
case is_string($value):
return _js_uc_esc($value);
default:
return $value;
}
}
function _js_error() {
return trigger_error(
'json_encode(): type is unsupported, encoded as null',
E_USER_WARNING
);
}
function _js_uc_esc($value) {
if ($value !== mb_convert_encoding("UTF-8", "UTF-8", $value)) {
return 'null';
}
$pattern = "@[\x08-x010\x0c\x0d\\\\\"/]|([^\x08-\x10\x0c\x0d\x32-\x7f\\\\\"/])++@";
return '"' . preg_replace_callback($pattern, '_js_uc_esc_cb', $value) . '"';
}
function _js_uc_esc_cb($matches) {
switch (true) {
case isset($matches[1]):
$char = mb_convert_encoding($matches[0], 'UTF-16', 'UTF-8');
$str = '';
$len = strlen($char);
for ($i = 0; $i < $len; $i += 2) {
$str .= sprintf('\\u%02x%02x',
ord($char[$i]),
ord($char[$i + 1])
);
}
return $str;
default:
return addcslashes($matches[0], "\x08\x09\x10\x0c\x0d\\\"/");
}
}
}
$str = '';
for ($i=0;$i<=300;$i++) {
$str .= chr($i);
}
$str .= chr(127);
var_dump(json_encode(array("test" => $str)));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 bool(false)
Output for 5.2.7 - 5.2.17 , 5.3.0 - 5.3.13 , 5.3.15 - 5.3.29 , 5.4.0 - 5.4.45 string(13) "{"test":null}"
Output for 5.3.14 Warning: json_encode(): Invalid UTF-8 sequence in argument in /in/PtNV1 on line 97
bool(false)
Output for 5.2.2 - 5.2.6 string(282) "{"test":"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&'()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"}"
Output for 5.2.0 - 5.2.1 string(147) "{"test":" \b\t\n\f\r !\"#$%&'()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"}"
Output for 4.3.2 - 4.3.11 , 4.4.0 - 4.4.9 , 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 Warning: mb_convert_encoding(): Illegal character encoding specified in /in/PtNV1 on line 65
Warning: mb_convert_encoding(): Illegal character encoding specified in /in/PtNV1 on line 65
string(11) "{null:null}"
Output for 4.3.0 - 4.3.1 Warning: mb_convert_encoding() [http://www.php.net/function.mb-convert-encoding]: Illegal character encoding specified in /in/PtNV1 on line 65
Warning: mb_convert_encoding() [http://www.php.net/function.mb-convert-encoding]: Illegal character encoding specified in /in/PtNV1 on line 65
string(11) "{null:null}"
preferences:dark mode live preview
264.87 ms | 402 KiB | 457 Q