3v4l.org

run code in 300+ PHP versions simultaneously
<?php function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if($suffix) $suffixStr = "…"; else $suffixStr = ""; if(function_exists("mb_substr")) return mb_substr($str, $start, $length, $charset).$suffixStr; elseif(function_exists('iconv_substr')) { return iconv_substr($str,$start,$length,$charset).$suffixStr; } $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match); $slice = join("",array_slice($match[0], $start, $length)); return $slice.$suffixStr; } function GBsubstr($string, $start, $length) { if(strlen($string)>$length){ $str=null; $len=$start+$length; for($i=$start;$i<$len;$i++){ if(ord(substr($string,$i,1))>0xa0){ $str.=substr($string,$i,2); $i++; }else{ $str.=substr($string,$i,1); } } return $str.'...'; }else{ return $string; } } $a = '测试这是一个测试'; //$a = 'xxxxbbbbbnnn'; $b = GBsubstr($a,0,5); var_dump($b); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lNXl7
function name:  (null)
number of ops:  11
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   ASSIGN                                                   !0, '%E6%B5%8B%E8%AF%95%E8%BF%99%E6%98%AF%E4%B8%80%E4%B8%AA%E6%B5%8B%E8%AF%95'
   45     1        INIT_FCALL                                               'gbsubstr'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 0
          4        SEND_VAL                                                 5
          5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !1, $3
   46     7        INIT_FCALL                                               'var_dump'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                                 
   48    10      > RETURN                                                   1

Function msubstr:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 22
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 34
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 22
Branch analysis from position: 13
Branch analysis from position: 22
filename:       /in/lNXl7
function name:  msubstr
number of ops:  62
compiled vars:  !0 = $str, !1 = $start, !2 = $length, !3 = $charset, !4 = $suffix, !5 = $suffixStr, !6 = $re, !7 = $match, !8 = $slice
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      'utf-8'
          4        RECV_INIT                                        !4      <true>
    5     5      > JMPZ                                                     !4, ->8
    6     6    >   ASSIGN                                                   !5, '%E2%80%A6'
          7      > JMP                                                      ->9
    8     8    >   ASSIGN                                                   !5, ''
   10     9    >   INIT_FCALL                                               'function_exists'
         10        SEND_VAL                                                 'mb_substr'
         11        DO_ICALL                                         $11     
         12      > JMPZ                                                     $11, ->22
   11    13    >   INIT_FCALL                                               'mb_substr'
         14        SEND_VAR                                                 !0
         15        SEND_VAR                                                 !1
         16        SEND_VAR                                                 !2
         17        SEND_VAR                                                 !3
         18        DO_ICALL                                         $12     
         19        CONCAT                                           ~13     $12, !5
         20      > RETURN                                                   ~13
         21*       JMP                                                      ->34
   12    22    >   INIT_FCALL                                               'function_exists'
         23        SEND_VAL                                                 'iconv_substr'
         24        DO_ICALL                                         $14     
         25      > JMPZ                                                     $14, ->34
   13    26    >   INIT_FCALL_BY_NAME                                       'iconv_substr'
         27        SEND_VAR_EX                                              !0
         28        SEND_VAR_EX                                              !1
         29        SEND_VAR_EX                                              !2
         30        SEND_VAR_EX                                              !3
         31        DO_FCALL                                      0  $15     
         32        CONCAT                                           ~16     $15, !5
         33      > RETURN                                                   ~16
   15    34    >   ASSIGN_DIM                                               !6, 'utf-8'
         35        OP_DATA                                                  '%2F%5B%01-%7F%5D%7C%5B%C2-%DF%5D%5B%80-%BF%5D%7C%5B%E0-%EF%5D%5B%80-%BF%5D%7B2%7D%7C%5B%F0-%FF%5D%5B%80-%BF%5D%7B3%7D%2F'
   16    36        ASSIGN_DIM                                               !6, 'gb2312'
         37        OP_DATA                                                  '%2F%5B%01-%7F%5D%7C%5B%B0-%F7%5D%5B%A0-%FE%5D%2F'
   17    38        ASSIGN_DIM                                               !6, 'gbk'
         39        OP_DATA                                                  '%2F%5B%01-%7F%5D%7C%5B%81-%FE%5D%5B%40-%FE%5D%2F'
   18    40        ASSIGN_DIM                                               !6, 'big5'
         41        OP_DATA                                                  '%2F%5B%01-%7F%5D%7C%5B%81-%FE%5D%28%5B%40-%7E%5D%7C%A1-%FE%5D%29%2F'
   19    42        INIT_FCALL                                               'preg_match_all'
         43        FETCH_DIM_R                                      ~21     !6, !3
         44        SEND_VAL                                                 ~21
         45        SEND_VAR                                                 !0
         46        SEND_REF                                                 !7
         47        DO_ICALL                                                 
   20    48        INIT_FCALL                                               'join'
         49        SEND_VAL                                                 ''
         50        INIT_FCALL                                               'array_slice'
         51        FETCH_DIM_R                                      ~23     !7, 0
         52        SEND_VAL                                                 ~23
         53        SEND_VAR                                                 !1
         54        SEND_VAR                                                 !2
         55        DO_ICALL                                         $24     
         56        SEND_VAR                                                 $24
         57        DO_ICALL                                         $25     
         58        ASSIGN                                                   !8, $25
   21    59        CONCAT                                           ~27     !8, !5
         60      > RETURN                                                   ~27
   22    61*     > RETURN                                                   null

End of function msubstr

Function gbsubstr:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 41
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 11
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 29
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 11
Branch analysis from position: 38
Branch analysis from position: 11
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 38, Position 2 = 11
Branch analysis from position: 38
Branch analysis from position: 11
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lNXl7
function name:  GBsubstr
number of ops:  43
compiled vars:  !0 = $string, !1 = $start, !2 = $length, !3 = $str, !4 = $len, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   25     3        STRLEN                                           ~6      !0
          4        IS_SMALLER                                               !2, ~6
          5      > JMPZ                                                     ~7, ->41
   26     6    >   ASSIGN                                                   !3, null
   27     7        ADD                                              ~9      !1, !2
          8        ASSIGN                                                   !4, ~9
   28     9        ASSIGN                                                   !5, !1
         10      > JMP                                                      ->36
   29    11    >   INIT_FCALL                                               'ord'
         12        INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !5
         15        SEND_VAL                                                 1
         16        DO_ICALL                                         $12     
         17        SEND_VAR                                                 $12
         18        DO_ICALL                                         $13     
         19        IS_SMALLER                                               160, $13
         20      > JMPZ                                                     ~14, ->29
   30    21    >   INIT_FCALL                                               'substr'
         22        SEND_VAR                                                 !0
         23        SEND_VAR                                                 !5
         24        SEND_VAL                                                 2
         25        DO_ICALL                                         $15     
         26        ASSIGN_OP                                     8          !3, $15
   31    27        PRE_INC                                                  !5
         28      > JMP                                                      ->35
   33    29    >   INIT_FCALL                                               'substr'
         30        SEND_VAR                                                 !0
         31        SEND_VAR                                                 !5
         32        SEND_VAL                                                 1
         33        DO_ICALL                                         $18     
         34        ASSIGN_OP                                     8          !3, $18
   28    35    >   PRE_INC                                                  !5
         36    >   IS_SMALLER                                               !5, !4
         37      > JMPNZ                                                    ~21, ->11
   36    38    >   CONCAT                                           ~22     !3, '...'
         39      > RETURN                                                   ~22
         40*       JMP                                                      ->42
   38    41    > > RETURN                                                   !0
   40    42*     > RETURN                                                   null

End of function gbsubstr

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.5 ms | 1411 KiB | 33 Q