3v4l.org

run code in 300+ PHP versions simultaneously
<?php function c_substr($string, $from, $length = null){ preg_match_all('/[x80-xff]?./', $string, $match); if(is_null($length)){ $result = implode('', array_slice($match[0], $from)); }else{ $result = implode('', array_slice($match[0], $from, $length)); } return $result; } $str = "zhon华人min共和guo"; $from = 3; $length = 7; echo(cut_str($str, $length)); function cut_str($sourcestr,$cutlength) { $returnstr=''; $i=0; $n=0; $str_length=strlen($sourcestr);//字符串的字节数 while (($n<$cutlength) and ($i<=$str_length)) { $temp_str=substr($sourcestr,$i,1); $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码 if ($ascnum>=224) //如果ASCII位高与224, { $returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符 $i=$i+3; //实际Byte计为3 $n++; //字串长度计1 } elseif ($ascnum>=192) //如果ASCII位高与192, { $returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符 $i=$i+2; //实际Byte计为2 $n++; //字串长度计1 } elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母, { $returnstr=$returnstr.substr($sourcestr,$i,1); $i=$i+1; //实际的Byte数仍计1个 $n++; //但考虑整体美观,大写字母计成一个高位字符 } else //其他情况下,包括小写字母和半角标点符号, { $returnstr=$returnstr.substr($sourcestr,$i,1); $i=$i+1; //实际的Byte数计1个 $n=$n+0.5; //小写字母和半角标点等与半个高位字符宽... } } if ($str_length>$cutlength){ $returnstr = $returnstr . "...";//超过长度时在尾处加上省略号 } return $returnstr; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3J4Dr
function name:  (null)
number of ops:  9
compiled vars:  !0 = $str, !1 = $from, !2 = $length
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   ASSIGN                                                   !0, 'zhon%E5%8D%8E%E4%BA%BAmin%E5%85%B1%E5%92%8Cguo'
   12     1        ASSIGN                                                   !1, 3
   13     2        ASSIGN                                                   !2, 7
   14     3        INIT_FCALL_BY_NAME                                       'cut_str'
          4        SEND_VAR_EX                                              !0
          5        SEND_VAR_EX                                              !2
          6        DO_FCALL                                      0  $6      
          7        ECHO                                                     $6
   55     8      > RETURN                                                   1

Function c_substr:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 21
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3J4Dr
function name:  c_substr
number of ops:  34
compiled vars:  !0 = $string, !1 = $from, !2 = $length, !3 = $match, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
    3     3        INIT_FCALL                                               'preg_match_all'
          4        SEND_VAL                                                 '%2F%5Bx80-xff%5D%3F.%2F'
          5        SEND_VAR                                                 !0
          6        SEND_REF                                                 !3
          7        DO_ICALL                                                 
    4     8        TYPE_CHECK                                    2          !2
          9      > JMPZ                                                     ~6, ->21
    5    10    >   INIT_FCALL                                               'implode'
         11        SEND_VAL                                                 ''
         12        INIT_FCALL                                               'array_slice'
         13        FETCH_DIM_R                                      ~7      !3, 0
         14        SEND_VAL                                                 ~7
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                         $8      
         17        SEND_VAR                                                 $8
         18        DO_ICALL                                         $9      
         19        ASSIGN                                                   !4, $9
         20      > JMP                                                      ->32
    7    21    >   INIT_FCALL                                               'implode'
         22        SEND_VAL                                                 ''
         23        INIT_FCALL                                               'array_slice'
         24        FETCH_DIM_R                                      ~11     !3, 0
         25        SEND_VAL                                                 ~11
         26        SEND_VAR                                                 !1
         27        SEND_VAR                                                 !2
         28        DO_ICALL                                         $12     
         29        SEND_VAR                                                 $12
         30        DO_ICALL                                         $13     
         31        ASSIGN                                                   !4, $13
    9    32    > > RETURN                                                   !4
   10    33*     > RETURN                                                   null

End of function c_substr

Function cut_str:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
2 jumps found. (Code = 46) Position 1 = 73, Position 2 = 75
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 76, Position 2 = 8
Branch analysis from position: 76
2 jumps found. (Code = 43) Position 1 = 78, Position 2 = 80
Branch analysis from position: 78
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 80
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 31
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 44
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
Branch analysis from position: 44
2 jumps found. (Code = 46) Position 1 = 46, Position 2 = 48
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 60
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 71
Branch analysis from position: 71
Branch analysis from position: 60
2 jumps found. (Code = 46) Position 1 = 73, Position 2 = 75
Branch analysis from position: 73
Branch analysis from position: 75
Branch analysis from position: 48
Branch analysis from position: 75
filename:       /in/3J4Dr
function name:  cut_str
number of ops:  82
compiled vars:  !0 = $sourcestr, !1 = $cutlength, !2 = $returnstr, !3 = $i, !4 = $n, !5 = $str_length, !6 = $temp_str, !7 = $ascnum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   17     2        ASSIGN                                                   !2, ''
   18     3        ASSIGN                                                   !3, 0
   19     4        ASSIGN                                                   !4, 0
   20     5        STRLEN                                           ~11     !0
          6        ASSIGN                                                   !5, ~11
   21     7      > JMP                                                      ->71
   23     8    >   INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !0
         10        SEND_VAR                                                 !3
         11        SEND_VAL                                                 1
         12        DO_ICALL                                         $13     
         13        ASSIGN                                                   !6, $13
   24    14        INIT_FCALL                                               'ord'
         15        SEND_VAR                                                 !6
         16        DO_ICALL                                         $15     
         17        ASSIGN                                                   !7, $15
   25    18        IS_SMALLER_OR_EQUAL                                      224, !7
         19      > JMPZ                                                     ~17, ->31
   27    20    >   INIT_FCALL                                               'substr'
         21        SEND_VAR                                                 !0
         22        SEND_VAR                                                 !3
         23        SEND_VAL                                                 3
         24        DO_ICALL                                         $18     
         25        CONCAT                                           ~19     !2, $18
         26        ASSIGN                                                   !2, ~19
   28    27        ADD                                              ~21     !3, 3
         28        ASSIGN                                                   !3, ~21
   29    29        PRE_INC                                                  !4
         30      > JMP                                                      ->71
   31    31    >   IS_SMALLER_OR_EQUAL                                      192, !7
         32      > JMPZ                                                     ~24, ->44
   33    33    >   INIT_FCALL                                               'substr'
         34        SEND_VAR                                                 !0
         35        SEND_VAR                                                 !3
         36        SEND_VAL                                                 2
         37        DO_ICALL                                         $25     
         38        CONCAT                                           ~26     !2, $25
         39        ASSIGN                                                   !2, ~26
   34    40        ADD                                              ~28     !3, 2
         41        ASSIGN                                                   !3, ~28
   35    42        PRE_INC                                                  !4
         43      > JMP                                                      ->71
   37    44    >   IS_SMALLER_OR_EQUAL                              ~31     65, !7
         45      > JMPZ_EX                                          ~31     ~31, ->48
         46    >   IS_SMALLER_OR_EQUAL                              ~32     !7, 90
         47        BOOL                                             ~31     ~32
         48    > > JMPZ                                                     ~31, ->60
   39    49    >   INIT_FCALL                                               'substr'
         50        SEND_VAR                                                 !0
         51        SEND_VAR                                                 !3
         52        SEND_VAL                                                 1
         53        DO_ICALL                                         $33     
         54        CONCAT                                           ~34     !2, $33
         55        ASSIGN                                                   !2, ~34
   40    56        ADD                                              ~36     !3, 1
         57        ASSIGN                                                   !3, ~36
   41    58        PRE_INC                                                  !4
         59      > JMP                                                      ->71
   45    60    >   INIT_FCALL                                               'substr'
         61        SEND_VAR                                                 !0
         62        SEND_VAR                                                 !3
         63        SEND_VAL                                                 1
         64        DO_ICALL                                         $39     
         65        CONCAT                                           ~40     !2, $39
         66        ASSIGN                                                   !2, ~40
   46    67        ADD                                              ~42     !3, 1
         68        ASSIGN                                                   !3, ~42
   47    69        ADD                                              ~44     !4, 0.5
         70        ASSIGN                                                   !4, ~44
   21    71    >   IS_SMALLER                                       ~46     !4, !1
         72      > JMPZ_EX                                          ~46     ~46, ->75
         73    >   IS_SMALLER_OR_EQUAL                              ~47     !3, !5
         74        BOOL                                             ~46     ~47
         75    > > JMPNZ                                                    ~46, ->8
   50    76    >   IS_SMALLER                                               !1, !5
         77      > JMPZ                                                     ~48, ->80
   51    78    >   CONCAT                                           ~49     !2, '...'
         79        ASSIGN                                                   !2, ~49
   53    80    > > RETURN                                                   !2
   55    81*     > RETURN                                                   null

End of function cut_str

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.71 ms | 1408 KiB | 23 Q