[wp-trac] [WordPress Trac] #30471: json_encode() fails on non-utf8 strings

WordPress Trac noreply at wordpress.org
Mon Nov 24 01:47:18 UTC 2014


#30471: json_encode() fails on non-utf8 strings
--------------------------+-----------------------------
 Reporter:  pento         |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 In PHP 5.4 and earlier, when `json_encode()` is run on a string that
 contains non-UTF-8 characters, it will return the string `'null'`. As of
 PHP 5.5, the string will be converted to UTF-8 before it's escaped for the
 JSON output.

 Given this example:

 {{{
 $charsets = mb_detect_order();
 if ( ! in_array( 'EUC-JP', $charsets ) ) {
         $charsets[] = 'EUC-JP';
         mb_detect_order( $charsets );
 }

 $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' );
 $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' );

 $json = wp_json_encode( $eucjp );

 }}}

 `$json` will be the string `'null'` in PHP 5.4 an older, and the string
 `"a\u3042b"'` in PHP 5.5+.

 This becomes more complex as we try to encode arrays or objects.

 {{{
 $json = wp_json_encode( array( 'c', $eucjp ) );
 }}}

 Here, `$json` will be the string `'["c",null]'` in PHP 5.4 an older, and
 the string `'["c","a\u3042b"]'` in PHP 5.5+.

 It's fairly simple to fix this in `wp_json_encode()` for strings, and
 horrible to try and fix it for arbitrary arrays/objects.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/30471>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list