3v4l.org

run code in 300+ PHP versions simultaneously
<?php function unicode_encode($str, $encoding = 'GBK', $prefix = '&#', $postfix = ';') { $str = iconv($encoding, 'UCS-2', $str); $arrstr = str_split($str, 2); $unistr = ''; for($i = 0, $len = count($arrstr); $i < $len; $i++) { $dec = hexdec(bin2hex($arrstr[$i])); $unistr .= $prefix . $dec . $postfix; } return $unistr; } /** * $str Unicode编码后的字符串 * $decoding 原始字符串的编码,默认GBK * $prefix 编码字符串的前缀,默认"&#" * $postfix 编码字符串的后缀,默认";" */ function unicode_decode($unistr, $encoding = 'GBK', $prefix = '&#', $postfix = ';') { $arruni = explode($prefix, $unistr); $unistr = ''; for($i = 1, $len = count($arruni); $i < $len; $i++) { if (strlen($postfix) > 0) { $arruni[$i] = substr($arruni[$i], 0, strlen($arruni[$i]) - strlen($postfix)); } $temp = intval($arruni[$i]); $unistr .= ($temp < 256) ? chr(0) . chr($temp) : chr($temp / 256) . chr($temp % 256); } return iconv('UCS-2', $encoding, $unistr); } //GBK字符串测试 $str = '<b>哈哈</b>'; echo $str.'<br />'; $unistr = unicode_encode($str); echo $unistr.'<br />'; // &#60;&#98;&#62;&#21704;&#21704;&#60;&#47;&#98;&#62; $str2 = unicode_decode($unistr); echo $str2.'<br />'; //<b>哈哈</b> //UTF-8字符串测试 $utf8_str = iconv('GBK', 'UTF-8', $str); echo $utf8_str.'<br />'; // <b>鍝堝搱</b> 注:UTF在GBK下显示的乱码!可切换浏览器的编码测试 $utf8_unistr = unicode_encode($utf8_str, 'UTF-8'); echo $utf8_unistr.'<br />'; // &#60;&#98;&#62;&#21704;&#21704;&#60;&#47;&#98;&#62; $utf8_str2 = unicode_decode($utf8_unistr, 'UTF-8'); echo $utf8_str2.'<br />'; // <b>鍝堝搱</b> //其它后缀、前缀测试 $prefix_unistr = unicode_encode($str, 'GBK', "\\u", ''); echo $prefix_unistr.'<br />'; // \u60\u98\u62\u21704\u21704\u60\u47\u98\u62 $profix_unistr2 = unicode_decode($prefix_unistr, 'GBK', "\\u", ''); echo $profix_unistr2.'<br />'; //<b>哈哈</b>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CD1Zn
function name:  (null)
number of ops:  56
compiled vars:  !0 = $str, !1 = $unistr, !2 = $str2, !3 = $utf8_str, !4 = $utf8_unistr, !5 = $utf8_str2, !6 = $prefix_unistr, !7 = $profix_unistr2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   ASSIGN                                                   !0, '%3Cb%3E%E5%93%88%E5%93%88%3C%2Fb%3E'
   34     1        CONCAT                                           ~9      !0, '%3Cbr+%2F%3E'
          2        ECHO                                                     ~9
   36     3        INIT_FCALL                                               'unicode_encode'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $10     
          6        ASSIGN                                                   !1, $10
   37     7        CONCAT                                           ~12     !1, '%3Cbr+%2F%3E'
          8        ECHO                                                     ~12
   39     9        INIT_FCALL                                               'unicode_decode'
         10        SEND_VAR                                                 !1
         11        DO_FCALL                                      0  $13     
         12        ASSIGN                                                   !2, $13
   40    13        CONCAT                                           ~15     !2, '%3Cbr+%2F%3E'
         14        ECHO                                                     ~15
   43    15        INIT_FCALL_BY_NAME                                       'iconv'
         16        SEND_VAL_EX                                              'GBK'
         17        SEND_VAL_EX                                              'UTF-8'
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0  $16     
         20        ASSIGN                                                   !3, $16
   44    21        CONCAT                                           ~18     !3, '%3Cbr+%2F%3E'
         22        ECHO                                                     ~18
   46    23        INIT_FCALL                                               'unicode_encode'
         24        SEND_VAR                                                 !3
         25        SEND_VAL                                                 'UTF-8'
         26        DO_FCALL                                      0  $19     
         27        ASSIGN                                                   !4, $19
   47    28        CONCAT                                           ~21     !4, '%3Cbr+%2F%3E'
         29        ECHO                                                     ~21
   49    30        INIT_FCALL                                               'unicode_decode'
         31        SEND_VAR                                                 !4
         32        SEND_VAL                                                 'UTF-8'
         33        DO_FCALL                                      0  $22     
         34        ASSIGN                                                   !5, $22
   50    35        CONCAT                                           ~24     !5, '%3Cbr+%2F%3E'
         36        ECHO                                                     ~24
   53    37        INIT_FCALL                                               'unicode_encode'
         38        SEND_VAR                                                 !0
         39        SEND_VAL                                                 'GBK'
         40        SEND_VAL                                                 '%5Cu'
         41        SEND_VAL                                                 ''
         42        DO_FCALL                                      0  $25     
         43        ASSIGN                                                   !6, $25
   54    44        CONCAT                                           ~27     !6, '%3Cbr+%2F%3E'
         45        ECHO                                                     ~27
   56    46        INIT_FCALL                                               'unicode_decode'
         47        SEND_VAR                                                 !6
         48        SEND_VAL                                                 'GBK'
         49        SEND_VAL                                                 '%5Cu'
         50        SEND_VAL                                                 ''
         51        DO_FCALL                                      0  $28     
         52        ASSIGN                                                   !7, $28
   57    53        CONCAT                                           ~30     !7, '%3Cbr+%2F%3E'
         54        ECHO                                                     ~30
         55      > RETURN                                                   1

Function unicode_encode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 20
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 20
Branch analysis from position: 34
Branch analysis from position: 20
filename:       /in/CD1Zn
function name:  unicode_encode
number of ops:  36
compiled vars:  !0 = $str, !1 = $encoding, !2 = $prefix, !3 = $postfix, !4 = $arrstr, !5 = $unistr, !6 = $i, !7 = $len, !8 = $dec
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'GBK'
          2        RECV_INIT                                        !2      '%26%23'
          3        RECV_INIT                                        !3      '%3B'
    3     4        INIT_FCALL_BY_NAME                                       'iconv'
          5        SEND_VAR_EX                                              !1
          6        SEND_VAL_EX                                              'UCS-2'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $9      
          9        ASSIGN                                                   !0, $9
    4    10        INIT_FCALL                                               'str_split'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 2
         13        DO_ICALL                                         $11     
         14        ASSIGN                                                   !4, $11
    5    15        ASSIGN                                                   !5, ''
    6    16        ASSIGN                                                   !6, 0
         17        COUNT                                            ~15     !4
         18        ASSIGN                                                   !7, ~15
         19      > JMP                                                      ->32
    7    20    >   INIT_FCALL                                               'hexdec'
         21        INIT_FCALL                                               'bin2hex'
         22        FETCH_DIM_R                                      ~17     !4, !6
         23        SEND_VAL                                                 ~17
         24        DO_ICALL                                         $18     
         25        SEND_VAR                                                 $18
         26        DO_ICALL                                         $19     
         27        ASSIGN                                                   !8, $19
    8    28        CONCAT                                           ~21     !2, !8
         29        CONCAT                                           ~22     ~21, !3
         30        ASSIGN_OP                                     8          !5, ~22
    6    31        PRE_INC                                                  !6
         32    >   IS_SMALLER                                               !6, !7
         33      > JMPNZ                                                    ~25, ->20
   10    34    > > RETURN                                                   !5
   11    35*     > RETURN                                                   null

End of function unicode_encode

Function unicode_decode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 14
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 29
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 40
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 14
Branch analysis from position: 54
Branch analysis from position: 14
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 54, Position 2 = 14
Branch analysis from position: 54
Branch analysis from position: 14
Branch analysis from position: 29
filename:       /in/CD1Zn
function name:  unicode_decode
number of ops:  61
compiled vars:  !0 = $unistr, !1 = $encoding, !2 = $prefix, !3 = $postfix, !4 = $arruni, !5 = $i, !6 = $len, !7 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'GBK'
          2        RECV_INIT                                        !2      '%26%23'
          3        RECV_INIT                                        !3      '%3B'
   20     4        INIT_FCALL                                               'explode'
          5        SEND_VAR                                                 !2
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $8      
          8        ASSIGN                                                   !4, $8
   21     9        ASSIGN                                                   !0, ''
   22    10        ASSIGN                                                   !5, 1
         11        COUNT                                            ~12     !4
         12        ASSIGN                                                   !6, ~12
         13      > JMP                                                      ->52
   23    14    >   STRLEN                                           ~14     !3
         15        IS_SMALLER                                               0, ~14
         16      > JMPZ                                                     ~15, ->29
   24    17    >   INIT_FCALL                                               'substr'
         18        FETCH_DIM_R                                      ~17     !4, !5
         19        SEND_VAL                                                 ~17
         20        SEND_VAL                                                 0
         21        FETCH_DIM_R                                      ~18     !4, !5
         22        STRLEN                                           ~19     ~18
         23        STRLEN                                           ~20     !3
         24        SUB                                              ~21     ~19, ~20
         25        SEND_VAL                                                 ~21
         26        DO_ICALL                                         $22     
         27        ASSIGN_DIM                                               !4, !5
         28        OP_DATA                                                  $22
   26    29    >   FETCH_DIM_R                                      ~23     !4, !5
         30        CAST                                          4  ~24     ~23
         31        ASSIGN                                                   !7, ~24
   27    32        IS_SMALLER                                               !7, 256
         33      > JMPZ                                                     ~26, ->40
         34    >   INIT_FCALL                                               'chr'
         35        SEND_VAR                                                 !7
         36        DO_ICALL                                         $27     
         37        CONCAT                                           ~28     '%00', $27
         38        QM_ASSIGN                                        ~29     ~28
         39      > JMP                                                      ->50
         40    >   INIT_FCALL                                               'chr'
         41        DIV                                              ~30     !7, 256
         42        SEND_VAL                                                 ~30
         43        DO_ICALL                                         $31     
         44        INIT_FCALL                                               'chr'
         45        MOD                                              ~32     !7, 256
         46        SEND_VAL                                                 ~32
         47        DO_ICALL                                         $33     
         48        CONCAT                                           ~34     $31, $33
         49        QM_ASSIGN                                        ~29     ~34
         50    >   ASSIGN_OP                                     8          !0, ~29
   22    51        PRE_INC                                                  !5
         52    >   IS_SMALLER                                               !5, !6
         53      > JMPNZ                                                    ~37, ->14
   29    54    >   INIT_FCALL_BY_NAME                                       'iconv'
         55        SEND_VAL_EX                                              'UCS-2'
         56        SEND_VAR_EX                                              !1
         57        SEND_VAR_EX                                              !0
         58        DO_FCALL                                      0  $38     
         59      > RETURN                                                   $38
   30    60*     > RETURN                                                   null

End of function unicode_decode

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.02 ms | 1411 KiB | 31 Q