Last active
July 10, 2022 08:10
-
-
Save muhqu/863757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = (object)array( | |
"html" => "<foo bar=\"baz\"/> &", | |
"arabic" => "العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī", | |
"hebrew" => "עִבְרִית, Ivrit", | |
"chinese" => "汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén", | |
"korean" => "한국어/조선말", | |
"japanese" => "日本語 Nihongo", | |
"umlauts" => "äüöãáàß", | |
"escaped" => "\u65e5\u672c\u8a9e", | |
"emoji" => json_decode('"\u263a \ue415\ue056\ue057\ue414\ue405\ue106\ue418 \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"'), | |
); | |
header("Content-Type: text/plain; charset=UTF-8"); | |
print title("php: print_r(data)")."\n".print_r($data,true)."\n\n"; | |
print title("php: json_encode(data)")."\n".($json = json_encode($data))."\nstrlen: ".strlen($json)."\n\n\n"; | |
print title("php: json_encode_unicode(data)")."\n".($json = json_encode_unicode($data))."\nstrlen: ".strlen($json)."\n\n\n"; | |
print title("php: print_r(json_decode(json_encode_unicode(data)))")."\n".print_r(json_decode(json_encode_unicode($data)),true)."\n\n"; | |
print title("php: data == json_decode(json_encode_unicode(data))")."\n".print_r($data == json_decode(json_encode_unicode($data)),true)."\n\n"; | |
function json_encode_unicode($data) { | |
if (defined('JSON_UNESCAPED_UNICODE')) { | |
return json_encode($data, JSON_UNESCAPED_UNICODE); | |
} | |
return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i', | |
function ($m) { | |
$d = pack("H*", $m[1]); | |
$r = mb_convert_encoding($d, "UTF8", "UTF-16BE"); | |
return $r!=="?" && $r!=="" ? $r : $m[0]; | |
}, json_encode($data) | |
); | |
} | |
// some benchmarking... | |
print "\n\n".title("Benchmarks")."\n"; | |
gauge("json_encode", 1000, function() use ($data) { | |
json_encode($data); | |
}); | |
gauge("json_encode_unicode", 1000, function() use ($data) { | |
json_encode_unicode($data); | |
}); | |
// Utils... | |
function gauge($label, $times, $callback) { | |
$tstart = microtime(true); | |
for ($i=0; $i<$times; $i++) { | |
$callback(); | |
} | |
$tend = microtime(true); | |
$took = ($tend-$tstart); | |
print (ceil($took*1000)/1000)." to run $label run $times times\n"; | |
} | |
function title($str) { | |
return $str."\n".str_repeat("-",strlen($str)); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
php: print_r(data) | |
------------------ | |
stdClass Object | |
( | |
[html] => <foo bar="baz"/> & | |
[arabic] => العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī | |
[hebrew] => עִבְרִית, Ivrit | |
[chinese] => 汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén | |
[korean] => 한국어/조선말 | |
[japanese] => 日本語 Nihongo | |
[umlauts] => äüöãáàß | |
[escaped] => \u65e5\u672c\u8a9e | |
[emoji] => ☺ <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/263a.png' class='emoji' title='WHITE SMILING FACE' alt='WHITE SMILING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' > <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' > | |
) | |
php: json_encode(data) | |
---------------------- | |
{"html":"<foo bar=\"baz\"\/> &","arabic":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629 al-\u02bfarabiyyah, IPA: [\u00e6l \u0295\u0251r\u0251\u02c8bijj\u0250], or \u0639\u0631\u0628\u064a \u02bfarab\u012b","hebrew":"\u05e2\u05b4\u05d1\u05b0\u05e8\u05b4\u05d9\u05ea, Ivrit","chinese":"\u6c49\u8bed\/\u6f22\u8a9e Hanyu; \u534e\u8bed\/\u83ef\u8a9e Hu\u00e1y\u01d4; \u4e2d\u6587 Zh\u014dngw\u00e9n","korean":"\ud55c\uad6d\uc5b4\/\uc870\uc120\ub9d0","japanese":"\u65e5\u672c\u8a9e Nihongo","umlauts":"\u00e4\u00fc\u00f6\u00e3\u00e1\u00e0\u00df","escaped":"\\u65e5\\u672c\\u8a9e","emoji":"\u263a \ue415\ue056\ue057\ue414\ue405\ue106\ue418 \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"} | |
strlen: 708 | |
php: json_encode_unicode(data) | |
------------------------------ | |
{"html":"<foo bar=\"baz\"\/> &","arabic":"العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī","hebrew":"עִבְרִית, Ivrit","chinese":"汉语\/漢語 Hanyu; 华语\/華語 Huáyǔ; 中文 Zhōngwén","korean":"한국어\/조선말","japanese":"日本語 Nihongo","umlauts":"äüöãáàß","escaped":"\\u65e5\\u672c\\u8a9e","emoji":"☺ <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/263a.png' class='emoji' title='WHITE SMILING FACE' alt='WHITE SMILING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' > \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"} | |
strlen: 471 | |
php: print_r(json_decode(json_encode_unicode(data))) | |
---------------------------------------------------- | |
stdClass Object | |
( | |
[html] => <foo bar="baz"/> & | |
[arabic] => العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī | |
[hebrew] => עִבְרִית, Ivrit | |
[chinese] => 汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén | |
[korean] => 한국어/조선말 | |
[japanese] => 日本語 Nihongo | |
[umlauts] => äüöãáàß | |
[escaped] => \u65e5\u672c\u8a9e | |
[emoji] => ☺ <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/263a.png' class='emoji' title='WHITE SMILING FACE' alt='WHITE SMILING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' > <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' > | |
) | |
php: data == json_decode(json_encode_unicode(data)) | |
--------------------------------------------------- | |
1 | |
Benchmarks | |
---------- | |
0.01 to run json_encode run 1000 times | |
0.658 to run json_encode_unicode run 1000 times |
Thank you so much! It's really helpful.
I'm really Thankful for you , I spent more 2 hours to solve this support problem but I couldn't and finally I found your lib :)
Thank you
Thank you
awesome!
awesome!
Cool!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Many thanks for this!