Skip to content

Instantly share code, notes, and snippets.

@r3sult
Forked from muhqu/json_encode_unicode.php
Created July 24, 2019 21:34
Show Gist options
  • Select an option

  • Save r3sult/7e24162ddfd69a79f76441f70e169264 to your computer and use it in GitHub Desktop.

Select an option

Save r3sult/7e24162ddfd69a79f76441f70e169264 to your computer and use it in GitHub Desktop.

Revisions

  1. @muhqu muhqu revised this gist Jan 8, 2014. 2 changed files with 6 additions and 3 deletions.
    3 changes: 3 additions & 0 deletions json_encode_unicode.php
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,9 @@
    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]);
    6 changes: 3 additions & 3 deletions result
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ stdClass Object
    [japanese] => 日本語 Nihongo
    [umlauts] => äüöãáàß
    [escaped] => \u65e5\u672c\u8a9e
    [emoji] => ☺  😄😊😃😉😍😘
    [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' >
    )


    @@ -22,7 +22,7 @@ strlen: 708

    php: json_encode_unicode(data)
    ------------------------------
    {"html":"<foo bar=\"baz\"\/> &amp;","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":"☺  \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"}
    {"html":"<foo bar=\"baz\"\/> &amp;","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


    @@ -38,7 +38,7 @@ stdClass Object
    [japanese] => 日本語 Nihongo
    [umlauts] => äüöãáàß
    [escaped] => \u65e5\u672c\u8a9e
    [emoji] => ☺  😄😊😃😉😍😘
    [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' >
    )


  2. @muhqu muhqu revised this gist Sep 20, 2011. 2 changed files with 13 additions and 9 deletions.
    8 changes: 5 additions & 3 deletions json_encode_unicode.php
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@
    "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");
    @@ -25,9 +26,10 @@

    function json_encode_unicode($data) {
    return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i',
    function ($matches) {
    $d = pack("H*", $matches[1]);
    return mb_convert_encoding($d, "UTF-8", "UTF-16BE");
    function ($m) {
    $d = pack("H*", $m[1]);
    $r = mb_convert_encoding($d, "UTF8", "UTF-16BE");
    return $r!=="?" && $r!=="" ? $r : $m[0];
    }, json_encode($data)
    );
    }
    14 changes: 8 additions & 6 deletions result
    Original file line number Diff line number Diff line change
    @@ -10,19 +10,20 @@ stdClass Object
    [japanese] => 日本語 Nihongo
    [umlauts] => äüöãáàß
    [escaped] => \u65e5\u672c\u8a9e
    [emoji] => ☺  😄😊😃😉😍😘
    )


    php: json_encode(data)
    ----------------------
    {"html":"<foo bar=\"baz\"\/> &amp;","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"}
    strlen: 575
    {"html":"<foo bar=\"baz\"\/> &amp;","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\"\/> &amp;","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"}
    strlen: 362
    {"html":"<foo bar=\"baz\"\/> &amp;","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":"☺  \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"}
    strlen: 471


    php: print_r(json_decode(json_encode_unicode(data)))
    @@ -37,6 +38,7 @@ stdClass Object
    [japanese] => 日本語 Nihongo
    [umlauts] => äüöãáàß
    [escaped] => \u65e5\u672c\u8a9e
    [emoji] => ☺  😄😊😃😉😍😘
    )


    @@ -48,5 +50,5 @@ php: data == json_decode(json_encode_unicode(data))

    Benchmarks
    ----------
    0.008 to run json_encode run 1000 times
    0.333 to run json_encode_unicode run 1000 times
    0.01 to run json_encode run 1000 times
    0.658 to run json_encode_unicode run 1000 times
  3. @muhqu muhqu revised this gist Mar 14, 2011. 2 changed files with 17 additions and 22 deletions.
    21 changes: 7 additions & 14 deletions json_encode_unicode.php
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    <?php

    $data = (object)array(
    "html" => "<foo /> &amp; <bar />",
    "html" => "<foo bar=\"baz\"/> &amp;",
    "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",
    );

    @@ -23,19 +24,11 @@
    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) {
    return str_replace("&&","&",
    html_entity_decode(
    preg_replace(
    '/(?<!\\\\)\\\\u([0-9a-f]{4})/u',
    '&#x\\1;',
    //'[\\1]',
    str_replace("&","&&",
    json_encode($data)
    )
    ),
    ENT_NOQUOTES,
    'UTF-8'
    )
    return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i',
    function ($matches) {
    $d = pack("H*", $matches[1]);
    return mb_convert_encoding($d, "UTF-8", "UTF-16BE");
    }, json_encode($data)
    );
    }

    18 changes: 10 additions & 8 deletions result
    Original file line number Diff line number Diff line change
    @@ -2,38 +2,40 @@ php: print_r(data)
    ------------------
    stdClass Object
    (
    [html] => <foo /> &amp; <bar />
    [html] => <foo bar="baz"/> &amp;
    [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
    )


    php: json_encode(data)
    ----------------------
    {"html":"<foo \/> &amp; <bar \/>","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","escaped":"\\u65e5\\u672c\\u8a9e"}
    strlen: 518
    {"html":"<foo bar=\"baz\"\/> &amp;","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"}
    strlen: 575


    php: json_encode_unicode(data)
    ------------------------------
    {"html":"<foo \/> &amp; <bar \/>","arabic":"العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī","hebrew":"עִבְרִית, Ivrit","chinese":"汉语\/漢語 Hanyu; 华语\/華語 Huáyǔ; 中文 Zhōngwén","korean":"한국어\/조선말","japanese":"日本語 Nihongo","escaped":"\\u65e5\\u672c\\u8a9e"}
    strlen: 333
    {"html":"<foo bar=\"baz\"\/> &amp;","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"}
    strlen: 362


    php: print_r(json_decode(json_encode_unicode(data)))
    ----------------------------------------------------
    stdClass Object
    (
    [html] => <foo /> &amp; <bar />
    [html] => <foo bar="baz"/> &amp;
    [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
    )

    @@ -46,5 +48,5 @@ php: data == json_decode(json_encode_unicode(data))

    Benchmarks
    ----------
    0.009 to run json_encode run 1000 times
    0.493 to run json_encode_unicode run 1000 times
    0.008 to run json_encode run 1000 times
    0.333 to run json_encode_unicode run 1000 times
  4. @muhqu muhqu created this gist Mar 10, 2011.
    69 changes: 69 additions & 0 deletions json_encode_unicode.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    <?php

    $data = (object)array(
    "html" => "<foo /> &amp; <bar />",
    "arabic" => "العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī",
    "hebrew" => "עִבְרִית, Ivrit",
    "chinese" => "汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén",
    "korean" => "한국어/조선말",
    "japanese" => "日本語 Nihongo",
    "escaped" => "\u65e5\u672c\u8a9e",
    );

    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) {
    return str_replace("&&","&",
    html_entity_decode(
    preg_replace(
    '/(?<!\\\\)\\\\u([0-9a-f]{4})/u',
    '&#x\\1;',
    //'[\\1]',
    str_replace("&","&&",
    json_encode($data)
    )
    ),
    ENT_NOQUOTES,
    'UTF-8'
    )
    );
    }


    // 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));
    }

    ?>
    50 changes: 50 additions & 0 deletions result
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    php: print_r(data)
    ------------------
    stdClass Object
    (
    [html] => <foo /> &amp; <bar />
    [arabic] => العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī
    [hebrew] => עִבְרִית, Ivrit
    [chinese] => 汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén
    [korean] => 한국어/조선말
    [japanese] => 日本語 Nihongo
    [escaped] => \u65e5\u672c\u8a9e
    )


    php: json_encode(data)
    ----------------------
    {"html":"<foo \/> &amp; <bar \/>","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","escaped":"\\u65e5\\u672c\\u8a9e"}
    strlen: 518


    php: json_encode_unicode(data)
    ------------------------------
    {"html":"<foo \/> &amp; <bar \/>","arabic":"العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī","hebrew":"עִבְרִית, Ivrit","chinese":"汉语\/漢語 Hanyu; 华语\/華語 Huáyǔ; 中文 Zhōngwén","korean":"한국어\/조선말","japanese":"日本語 Nihongo","escaped":"\\u65e5\\u672c\\u8a9e"}
    strlen: 333


    php: print_r(json_decode(json_encode_unicode(data)))
    ----------------------------------------------------
    stdClass Object
    (
    [html] => <foo /> &amp; <bar />
    [arabic] => العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī
    [hebrew] => עִבְרִית, Ivrit
    [chinese] => 汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén
    [korean] => 한국어/조선말
    [japanese] => 日本語 Nihongo
    [escaped] => \u65e5\u672c\u8a9e
    )


    php: data == json_decode(json_encode_unicode(data))
    ---------------------------------------------------
    1



    Benchmarks
    ----------
    0.009 to run json_encode run 1000 times
    0.493 to run json_encode_unicode run 1000 times