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 u8_title_substr($str, $width = 0, $end = '...', $x3 = 0) { global $CFG; // 全局变量保存 x3 的值 if ($width <= 0 || $width >= strlen($str)) { return $str; } $arr = str_split($str); $len = count($arr); $w = 0; $width *= 10; // 不同字节编码字符宽度系数 $x1 = 11; // ASCII $x2 = 16; $x3 = $x3===0 ? ( $CFG['cf3'] > 0 ? $CFG['cf3']*10 : $x3 = 21 ) : $x3*10; $x4 = $x3; // http://zh.wikipedia.org/zh-cn/UTF8 for ($i = 0; $i < $len; $i++) { if ($w >= $width) { $e = $end; break; } $c = ord($arr[$i]); if ($c <= 127) { $w += $x1; } elseif ($c >= 192 && $c <= 223) { // 2字节头 $w += $x2; $i += 1; } elseif ($c >= 224 && $c <= 239) { // 3字节头 $w += $x3; $i += 2; } elseif ($c >= 240 && $c <= 247) { // 4字节头 $w += $x4; $i += 3; } } return implode('', array_slice($arr, 0, $i) ). $e; } $a = '测试这是一个测试'; //$a = 'xxxxbbbbbnnn'; $b = u8_title_substr($a,0,5,10); var_dump($b); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BsZbI
function name:  (null)
number of ops:  12
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     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'
   70     1        INIT_FCALL                                               'u8_title_substr'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 0
          4        SEND_VAL                                                 5
          5        SEND_VAL                                                 10
          6        DO_FCALL                                      0  $3      
          7        ASSIGN                                                   !1, $3
   71     8        INIT_FCALL                                               'var_dump'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                                 
   73    11      > 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/BsZbI
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 u8_title_substr:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 35
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
2 jumps found. (Code = 44) Position 1 = 80, Position 2 = 41
Branch analysis from position: 80
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 45
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 54
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 77
Branch analysis from position: 77
2 jumps found. (Code = 44) Position 1 = 80, Position 2 = 41
Branch analysis from position: 80
Branch analysis from position: 41
Branch analysis from position: 54
2 jumps found. (Code = 46) Position 1 = 56, Position 2 = 58
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 62
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 77
Branch analysis from position: 77
Branch analysis from position: 62
2 jumps found. (Code = 46) Position 1 = 64, Position 2 = 66
Branch analysis from position: 64
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 70
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 77
Branch analysis from position: 77
Branch analysis from position: 70
2 jumps found. (Code = 46) Position 1 = 72, Position 2 = 74
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 77
Branch analysis from position: 75
2 jumps found. (Code = 44) Position 1 = 80, Position 2 = 41
Branch analysis from position: 80
Branch analysis from position: 41
Branch analysis from position: 77
Branch analysis from position: 74
Branch analysis from position: 66
Branch analysis from position: 58
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
Branch analysis from position: 10
filename:       /in/BsZbI
function name:  u8_title_substr
number of ops:  92
compiled vars:  !0 = $str, !1 = $width, !2 = $end, !3 = $x3, !4 = $CFG, !5 = $arr, !6 = $len, !7 = $w, !8 = $x1, !9 = $x2, !10 = $x4, !11 = $i, !12 = $e, !13 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      '...'
          3        RECV_INIT                                        !3      0
   25     4        BIND_GLOBAL                                              !4, 'CFG'
   26     5        IS_SMALLER_OR_EQUAL                              ~14     !1, 0
          6      > JMPNZ_EX                                         ~14     ~14, ->10
          7    >   STRLEN                                           ~15     !0
          8        IS_SMALLER_OR_EQUAL                              ~16     ~15, !1
          9        BOOL                                             ~14     ~16
         10    > > JMPZ                                                     ~14, ->12
   27    11    > > RETURN                                                   !0
   29    12    >   INIT_FCALL                                               'str_split'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $17     
         15        ASSIGN                                                   !5, $17
   30    16        COUNT                                            ~19     !5
         17        ASSIGN                                                   !6, ~19
   31    18        ASSIGN                                                   !7, 0
   32    19        ASSIGN_OP                                     3          !1, 10
   35    20        ASSIGN                                                   !8, 11
   36    21        ASSIGN                                                   !9, 16
   37    22        IS_IDENTICAL                                             !3, 0
         23      > JMPZ                                                     ~25, ->35
         24    >   FETCH_DIM_R                                      ~26     !4, 'cf3'
         25        IS_SMALLER                                               0, ~26
         26      > JMPZ                                                     ~27, ->31
         27    >   FETCH_DIM_R                                      ~28     !4, 'cf3'
         28        MUL                                              ~29     ~28, 10
         29        QM_ASSIGN                                        ~30     ~29
         30      > JMP                                                      ->33
         31    >   ASSIGN                                           ~31     !3, 21
         32        QM_ASSIGN                                        ~30     ~31
         33    >   QM_ASSIGN                                        ~32     ~30
         34      > JMP                                                      ->37
         35    >   MUL                                              ~33     !3, 10
         36        QM_ASSIGN                                        ~32     ~33
         37    >   ASSIGN                                                   !3, ~32
   38    38        ASSIGN                                                   !10, !3
   41    39        ASSIGN                                                   !11, 0
         40      > JMP                                                      ->78
   42    41    >   IS_SMALLER_OR_EQUAL                                      !1, !7
         42      > JMPZ                                                     ~37, ->45
   43    43    >   ASSIGN                                                   !12, !2
   44    44      > JMP                                                      ->80
   46    45    >   INIT_FCALL                                               'ord'
         46        FETCH_DIM_R                                      ~39     !5, !11
         47        SEND_VAL                                                 ~39
         48        DO_ICALL                                         $40     
         49        ASSIGN                                                   !13, $40
   47    50        IS_SMALLER_OR_EQUAL                                      !13, 127
         51      > JMPZ                                                     ~42, ->54
   48    52    >   ASSIGN_OP                                     1          !7, !8
         53      > JMP                                                      ->77
   50    54    >   IS_SMALLER_OR_EQUAL                              ~44     192, !13
         55      > JMPZ_EX                                          ~44     ~44, ->58
         56    >   IS_SMALLER_OR_EQUAL                              ~45     !13, 223
         57        BOOL                                             ~44     ~45
         58    > > JMPZ                                                     ~44, ->62
   51    59    >   ASSIGN_OP                                     1          !7, !9
   52    60        ASSIGN_OP                                     1          !11, 1
         61      > JMP                                                      ->77
   54    62    >   IS_SMALLER_OR_EQUAL                              ~48     224, !13
         63      > JMPZ_EX                                          ~48     ~48, ->66
         64    >   IS_SMALLER_OR_EQUAL                              ~49     !13, 239
         65        BOOL                                             ~48     ~49
         66    > > JMPZ                                                     ~48, ->70
   55    67    >   ASSIGN_OP                                     1          !7, !3
   56    68        ASSIGN_OP                                     1          !11, 2
         69      > JMP                                                      ->77
   58    70    >   IS_SMALLER_OR_EQUAL                              ~52     240, !13
         71      > JMPZ_EX                                          ~52     ~52, ->74
         72    >   IS_SMALLER_OR_EQUAL                              ~53     !13, 247
         73        BOOL                                             ~52     ~53
         74    > > JMPZ                                                     ~52, ->77
   59    75    >   ASSIGN_OP                                     1          !7, !10
   60    76        ASSIGN_OP                                     1          !11, 3
   41    77    >   PRE_INC                                                  !11
         78    >   IS_SMALLER                                               !11, !6
         79      > JMPNZ                                                    ~57, ->41
   64    80    >   INIT_FCALL                                               'implode'
         81        SEND_VAL                                                 ''
         82        INIT_FCALL                                               'array_slice'
         83        SEND_VAR                                                 !5
         84        SEND_VAL                                                 0
         85        SEND_VAR                                                 !11
         86        DO_ICALL                                         $58     
         87        SEND_VAR                                                 $58
         88        DO_ICALL                                         $59     
         89        CONCAT                                           ~60     $59, !12
         90      > RETURN                                                   ~60
   65    91*     > RETURN                                                   null

End of function u8_title_substr

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.07 ms | 1415 KiB | 32 Q