3v4l.org

run code in 300+ PHP versions simultaneously
<?php class json { protected $_jsonDefect = true; public function __construct() { $this->_jsonDefect = version_compare(PHP_VERSION, '5.4.0', '<'); } public function jsonEncode($data) { // 如果php json支持不编码unicode,直接使用 if (!$this->_jsonDefect) { return json_encode($data, JSON_UNESCAPED_UNICODE); } // 否则,先递归urlencode,转成JSON格式后urldecode $proResult = $this->_preproccessJson($data); return urldecode(json_encode($proResult)); } /** * 预处理JSON数据,递归对数据进行urlencode处理,以免结果中文编码成unicode * * @param string|array $data * @return string|array */ protected function _preproccessJson($data) { if (!is_array($data)) { return urlencode($data); } $result = array(); foreach ($data as $key => $value) { $result[urlencode($key)] = is_array($value) ? $this->_preproccessJson($value) : urlencode($value); } return $result; } } $json = new json(); $arr = array('name'=>'中文','test'=>'ok','arr'=>array('key'=>'中文')); var_dump($json->jsonEncode($arr)); var_dump(json_encode($arr)); var_dump($json->jsonEncode('中文')); var_dump(json_encode('中文'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bCa8Z
function name:  (null)
number of ops:  29
compiled vars:  !0 = $json, !1 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   NEW                                              $2      'json'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   36     3        ASSIGN                                                   !1, <array>
   37     4        INIT_FCALL                                               'var_dump'
          5        INIT_METHOD_CALL                                         !0, 'jsonEncode'
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0  $6      
          8        SEND_VAR                                                 $6
          9        DO_ICALL                                                 
   38    10        INIT_FCALL                                               'var_dump'
         11        INIT_FCALL                                               'json_encode'
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $8      
         14        SEND_VAR                                                 $8
         15        DO_ICALL                                                 
   39    16        INIT_FCALL                                               'var_dump'
         17        INIT_METHOD_CALL                                         !0, 'jsonEncode'
         18        SEND_VAL_EX                                              '%E4%B8%AD%E6%96%87'
         19        DO_FCALL                                      0  $10     
         20        SEND_VAR                                                 $10
         21        DO_ICALL                                                 
   40    22        INIT_FCALL                                               'var_dump'
         23        INIT_FCALL                                               'json_encode'
         24        SEND_VAL                                                 '%E4%B8%AD%E6%96%87'
         25        DO_ICALL                                         $12     
         26        SEND_VAR                                                 $12
         27        DO_ICALL                                                 
         28      > RETURN                                                   1

Class json:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bCa8Z
function name:  __construct
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   INIT_FCALL                                               'version_compare'
          1        SEND_VAL                                                 '8.0.0'
          2        SEND_VAL                                                 '5.4.0'
          3        SEND_VAL                                                 '%3C'
          4        DO_ICALL                                         $1      
          5        ASSIGN_OBJ                                               '_jsonDefect'
          6        OP_DATA                                                  $1
    7     7      > RETURN                                                   null

End of function __construct

Function jsonencode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bCa8Z
function name:  jsonEncode
number of ops:  21
compiled vars:  !0 = $data, !1 = $proResult
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
   10     1        FETCH_OBJ_R                                      ~2      '_jsonDefect'
          2        BOOL_NOT                                         ~3      ~2
          3      > JMPZ                                                     ~3, ->9
   11     4    >   INIT_FCALL                                               'json_encode'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 256
          7        DO_ICALL                                         $4      
          8      > RETURN                                                   $4
   14     9    >   INIT_METHOD_CALL                                         '_preproccessJson'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $5      
         12        ASSIGN                                                   !1, $5
   15    13        INIT_FCALL                                               'urldecode'
         14        INIT_FCALL                                               'json_encode'
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                         $7      
         17        SEND_VAR                                                 $7
         18        DO_ICALL                                         $8      
         19      > RETURN                                                   $8
   16    20*     > RETURN                                                   null

End of function jsonencode

Function _preproccessjson:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 29
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 29
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
filename:       /in/bCa8Z
function name:  _preproccessJson
number of ops:  32
compiled vars:  !0 = $data, !1 = $result, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        TYPE_CHECK                                  128  ~4      !0
          2        BOOL_NOT                                         ~5      ~4
          3      > JMPZ                                                     ~5, ->8
   26     4    >   INIT_FCALL                                               'urlencode'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $6      
          7      > RETURN                                                   $6
   28     8    >   ASSIGN                                                   !1, <array>
   29     9      > FE_RESET_R                                       $8      !0, ->29
         10    > > FE_FETCH_R                                       ~9      $8, !2, ->29
         11    >   ASSIGN                                                   !3, ~9
   30    12        INIT_FCALL                                               'urlencode'
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                         $11     
         15        TYPE_CHECK                                  128          !2
         16      > JMPZ                                                     ~13, ->22
         17    >   INIT_METHOD_CALL                                         '_preproccessJson'
         18        SEND_VAR_EX                                              !2
         19        DO_FCALL                                      0  $14     
         20        QM_ASSIGN                                        ~15     $14
         21      > JMP                                                      ->26
         22    >   INIT_FCALL                                               'urlencode'
         23        SEND_VAR                                                 !2
         24        DO_ICALL                                         $16     
         25        QM_ASSIGN                                        ~15     $16
         26    >   ASSIGN_DIM                                               !1, $11
         27        OP_DATA                                                  ~15
   29    28      > JMP                                                      ->10
         29    >   FE_FREE                                                  $8
   32    30      > RETURN                                                   !1
   33    31*     > RETURN                                                   null

End of function _preproccessjson

End of class json.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.86 ms | 1404 KiB | 23 Q