Skip to content

Instantly share code, notes, and snippets.

@mozhu1024
Last active May 21, 2019 07:42
Show Gist options
  • Save mozhu1024/8d2d8677156f6abde3beba2fa9b13a1f to your computer and use it in GitHub Desktop.
Save mozhu1024/8d2d8677156f6abde3beba2fa9b13a1f to your computer and use it in GitHub Desktop.
[PHP是如何解析JSON的 - 测试文件] #paper #php #json
<?php
error_reporting(E_ALL | E_STRICT);
function op($var = "", $msg = "") {
if ($msg) {
echo "\t$msg \r\n";
}
print_r($var);
echo "\r\n";
}
function exceptionHandler(Exception $e) {
op($e->getMessage(), 'Exception');
}
set_exception_handler('exceptionHandler');
// Normal
op("===== Normal =====");
$str = '{"vk":"virink"}';
$obj = json_decode($str);
op($str);
op($obj);
// Hex \x00
op("===== Hex \\x00 ===== ");
// $str = '{"a":"vir\x00ink"}';
$str = '{"vk":"vir\x00ink"}';
// echo JSON_BIGINT_AS_STRING . ' - ' . JSON_OBJECT_AS_ARRAY;
$obj = json_decode($str);
op($str);
op($obj);
op(json_last_error_msg());
op();
$obj = [
"a" => "\x00",
];
$str = json_encode($obj);
op($obj);
op($str);
op();
// Unicode \u0000
op("===== Unicode \\u0000 ===== ");
$str = '{"vk":"v\u0000k"}';
$obj = json_decode($str);
op($str);
op($obj);
op();
// Unicode \u003e
op("===== Unicode \\u003e ===== ");
$str = '{"vk":"vir\u003eink"}';
$obj = json_decode($str);
op($str);
op($obj);
op();
// Unicode \u2027\u2028\u2029\u2030
op("===== Unicode \\u2027\\u2028\\u2029\\u2030 ===== ");
$str = '{"vk":"v\u2027\u2028\u2029\u2030k"}';
$obj = json_decode($str);
op($str);
op($obj);
op();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment