3v4l.org

run code in 300+ PHP versions simultaneously
<?php function escape_javascript_string($str) { $map = [ 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,0,0, // 49 0,0,0,0,0,0,0,0,1,1, 1,1,1,1,1,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,1,1,1,1,1,1,0,0,0, // 99 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, 0,0,0,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, // 149 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, // 199 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1, // 249 1,1,1,1,1,1,1, // 255 ]; // 文字エンコーディングはUTF-8 $mblen = mb_strlen($str, 'UTF-8'); $utf32 = bin2hex(mb_convert_encoding($str, 'UTF-32', 'UTF-8')); for ($i=0, $encoded=''; $i < $mblen; $i++) { $u = substr($utf32, $i*8, 8); $v = base_convert($u, 16, 10); if ($v < 256 && $map[$v]) { $encoded .= '\\x'.substr($u, 6,2); } else { $encoded .= mb_convert_encoding(hex2bin($u), 'UTF-8', 'UTF-32'); } } return $encoded; } // テストデータ作成 $convmap = [ 0x0, 0xffff, 0, 0xffff ]; $msg = 'あああ<>'; for ($i=0; $i < 1000; $i++) { // chr()では正しいUTF-8の128以上の文字を生成できないのでmb_decode_numericentity()を利用 $msg .= mb_decode_numericentity('&#'.$i.';', $convmap, 'UTF-8'); } // var_dump($msg); var_dump(escape_javascript_string($msg));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 4
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 4
Branch analysis from position: 15
Branch analysis from position: 4
filename:       /in/E25pq
function name:  (null)
number of ops:  22
compiled vars:  !0 = $convmap, !1 = $msg, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   ASSIGN                                                   !0, <array>
   49     1        ASSIGN                                                   !1, '%E3%81%82%E3%81%82%E3%81%82%3C%3E'
   50     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->13
   52     4    >   INIT_FCALL                                               'mb_decode_numericentity'
          5        CONCAT                                           ~6      '%26%23', !2
          6        CONCAT                                           ~7      ~6, '%3B'
          7        SEND_VAL                                                 ~7
          8        SEND_VAR                                                 !0
          9        SEND_VAL                                                 'UTF-8'
         10        DO_ICALL                                         $8      
         11        ASSIGN_OP                                     8          !1, $8
   50    12        PRE_INC                                                  !2
         13    >   IS_SMALLER                                               !2, 1000
         14      > JMPNZ                                                    ~11, ->4
   56    15    >   INIT_FCALL                                               'var_dump'
         16        INIT_FCALL                                               'escape_javascript_string'
         17        SEND_VAR                                                 !1
         18        DO_FCALL                                      0  $12     
         19        SEND_VAR                                                 $12
         20        DO_ICALL                                                 
         21      > RETURN                                                   1

Function escape_javascript_string:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
2 jumps found. (Code = 44) Position 1 = 57, Position 2 = 19
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 46) Position 1 = 34, Position 2 = 36
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 45
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
2 jumps found. (Code = 44) Position 1 = 57, Position 2 = 19
Branch analysis from position: 57
Branch analysis from position: 19
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 57, Position 2 = 19
Branch analysis from position: 57
Branch analysis from position: 19
Branch analysis from position: 36
filename:       /in/E25pq
function name:  escape_javascript_string
number of ops:  59
compiled vars:  !0 = $str, !1 = $map, !2 = $mblen, !3 = $utf32, !4 = $i, !5 = $encoded, !6 = $u, !7 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
    3     1        ASSIGN                                                   !1, <array>
   32     2        INIT_FCALL                                               'mb_strlen'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'UTF-8'
          5        DO_ICALL                                         $9      
          6        ASSIGN                                                   !2, $9
   33     7        INIT_FCALL                                               'bin2hex'
          8        INIT_FCALL                                               'mb_convert_encoding'
          9        SEND_VAR                                                 !0
         10        SEND_VAL                                                 'UTF-32'
         11        SEND_VAL                                                 'UTF-8'
         12        DO_ICALL                                         $11     
         13        SEND_VAR                                                 $11
         14        DO_ICALL                                         $12     
         15        ASSIGN                                                   !3, $12
   34    16        ASSIGN                                                   !4, 0
         17        ASSIGN                                                   !5, ''
         18      > JMP                                                      ->55
   35    19    >   INIT_FCALL                                               'substr'
         20        SEND_VAR                                                 !3
         21        MUL                                              ~16     !4, 8
         22        SEND_VAL                                                 ~16
         23        SEND_VAL                                                 8
         24        DO_ICALL                                         $17     
         25        ASSIGN                                                   !6, $17
   36    26        INIT_FCALL                                               'base_convert'
         27        SEND_VAR                                                 !6
         28        SEND_VAL                                                 16
         29        SEND_VAL                                                 10
         30        DO_ICALL                                         $19     
         31        ASSIGN                                                   !7, $19
   37    32        IS_SMALLER                                       ~21     !7, 256
         33      > JMPZ_EX                                          ~21     ~21, ->36
         34    >   FETCH_DIM_R                                      ~22     !1, !7
         35        BOOL                                             ~21     ~22
         36    > > JMPZ                                                     ~21, ->45
   38    37    >   INIT_FCALL                                               'substr'
         38        SEND_VAR                                                 !6
         39        SEND_VAL                                                 6
         40        SEND_VAL                                                 2
         41        DO_ICALL                                         $23     
         42        CONCAT                                           ~24     '%5Cx', $23
         43        ASSIGN_OP                                     8          !5, ~24
         44      > JMP                                                      ->54
   40    45    >   INIT_FCALL                                               'mb_convert_encoding'
         46        INIT_FCALL                                               'hex2bin'
         47        SEND_VAR                                                 !6
         48        DO_ICALL                                         $26     
         49        SEND_VAR                                                 $26
         50        SEND_VAL                                                 'UTF-8'
         51        SEND_VAL                                                 'UTF-32'
         52        DO_ICALL                                         $27     
         53        ASSIGN_OP                                     8          !5, $27
   34    54    >   PRE_INC                                                  !4
         55    >   IS_SMALLER                                               !4, !2
         56      > JMPNZ                                                    ~30, ->19
   43    57    > > RETURN                                                   !5
   44    58*     > RETURN                                                   null

End of function escape_javascript_string

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.01 ms | 1407 KiB | 30 Q