3v4l.org

run code in 300+ PHP versions simultaneously
<?php function setKeyWords($str,$word_arr=array()){ // 设置多字节字符内部编码为utf8 mb_internal_encoding("UTF-8"); // 创建一个跟字符串长度一致的数组,用0填充 $map=array_fill(0,mb_strlen($str)+1,0); // 遍历关键词数组,将关键词对应的map数组的位置上的数字置为1 foreach ($word_arr as $value) { $pos=-1; $pos_count=0; $pos_arr=array(); // 如果找到了这个关键词,就将这个词的位置存入位置数组中(来支持多次出现此关键词的情况) while(($pos=mb_strpos($str,$value,$pos+1))!==false){ $pos_arr[]=$pos; $pos_count++; } // 遍历数组,将对应位置置1 foreach ($pos_arr as $pos_val) { if($pos_val!==false){ $fill=array_fill($pos_val,mb_strlen($value),1); $map = array_replace($map,$fill); } } $pos=null; } // 遍历map数组,加入高亮代码 $flag=0; $position=-1; $result=""; // 结果数组 foreach ($map as $key => $value) { if($value==1){ // 如果第一次出现1,则加上html标签头 if($flag==0) $result.="<span class=\"fc-red\">"; $flag=1; }else{ // 如果已经到了一个0,但上一个还是1时,加入html标签尾 if($flag==1){ $position=$key-1; $flag=0; $result.="</span>"; } } // 将该位置的字符加入结果字符串中 $result.=mb_substr($str,$key,1); } return $result; } $title="服务员服务啦"; $arr=array("服务","服务员"); echo setKeyWords($title,$arr);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XtEHI
function name:  (null)
number of ops:  8
compiled vars:  !0 = $title, !1 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   ASSIGN                                                   !0, '%E6%9C%8D%E5%8A%A1%E5%91%98%E6%9C%8D%E5%8A%A1%E5%95%A6'
   49     1        ASSIGN                                                   !1, <array>
   50     2        INIT_FCALL                                               'setkeywords'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
          7      > RETURN                                                   1

Function setkeywords:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 55
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 55
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 21
Branch analysis from position: 33
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 52
Branch analysis from position: 34
2 jumps found. (Code = 78) Position 1 = 35, Position 2 = 52
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 51
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 51
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 52
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 21
Branch analysis from position: 33
Branch analysis from position: 21
Branch analysis from position: 55
2 jumps found. (Code = 77) Position 1 = 60, Position 2 = 82
Branch analysis from position: 60
2 jumps found. (Code = 78) Position 1 = 61, Position 2 = 82
Branch analysis from position: 61
2 jumps found. (Code = 43) Position 1 = 64, Position 2 = 69
Branch analysis from position: 64
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 67
Branch analysis from position: 66
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 67
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 71, Position 2 = 75
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 75
Branch analysis from position: 82
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 82
Branch analysis from position: 55
filename:       /in/XtEHI
function name:  setKeyWords
number of ops:  85
compiled vars:  !0 = $str, !1 = $word_arr, !2 = $map, !3 = $value, !4 = $pos, !5 = $pos_count, !6 = $pos_arr, !7 = $pos_val, !8 = $fill, !9 = $flag, !10 = $position, !11 = $result, !12 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
    4     2        INIT_FCALL                                               'mb_internal_encoding'
          3        SEND_VAL                                                 'UTF-8'
          4        DO_ICALL                                                 
    6     5        INIT_FCALL                                               'array_fill'
          6        SEND_VAL                                                 0
          7        INIT_FCALL                                               'mb_strlen'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $14     
         10        ADD                                              ~15     $14, 1
         11        SEND_VAL                                                 ~15
         12        SEND_VAL                                                 0
         13        DO_ICALL                                         $16     
         14        ASSIGN                                                   !2, $16
    8    15      > FE_RESET_R                                       $18     !1, ->55
         16    > > FE_FETCH_R                                               $18, !3, ->55
    9    17    >   ASSIGN                                                   !4, -1
   10    18        ASSIGN                                                   !5, 0
   11    19        ASSIGN                                                   !6, <array>
   13    20      > JMP                                                      ->24
   14    21    >   ASSIGN_DIM                                               !6
         22        OP_DATA                                                  !4
   15    23        PRE_INC                                                  !5
   13    24    >   INIT_FCALL                                               'mb_strpos'
         25        SEND_VAR                                                 !0
         26        SEND_VAR                                                 !3
         27        ADD                                              ~24     !4, 1
         28        SEND_VAL                                                 ~24
         29        DO_ICALL                                         $25     
         30        ASSIGN                                           ~26     !4, $25
         31        TYPE_CHECK                                  1018          ~26
         32      > JMPNZ                                                    ~27, ->21
   18    33    > > FE_RESET_R                                       $28     !6, ->52
         34    > > FE_FETCH_R                                               $28, !7, ->52
   19    35    >   TYPE_CHECK                                  1018          !7
         36      > JMPZ                                                     ~29, ->51
   20    37    >   INIT_FCALL                                               'array_fill'
         38        SEND_VAR                                                 !7
         39        INIT_FCALL                                               'mb_strlen'
         40        SEND_VAR                                                 !3
         41        DO_ICALL                                         $30     
         42        SEND_VAR                                                 $30
         43        SEND_VAL                                                 1
         44        DO_ICALL                                         $31     
         45        ASSIGN                                                   !8, $31
   21    46        INIT_FCALL                                               'array_replace'
         47        SEND_VAR                                                 !2
         48        SEND_VAR                                                 !8
         49        DO_ICALL                                         $33     
         50        ASSIGN                                                   !2, $33
   18    51    > > JMP                                                      ->34
         52    >   FE_FREE                                                  $28
   24    53        ASSIGN                                                   !4, null
    8    54      > JMP                                                      ->16
         55    >   FE_FREE                                                  $18
   27    56        ASSIGN                                                   !9, 0
   28    57        ASSIGN                                                   !10, -1
   29    58        ASSIGN                                                   !11, ''
   30    59      > FE_RESET_R                                       $39     !2, ->82
         60    > > FE_FETCH_R                                       ~40     $39, !3, ->82
         61    >   ASSIGN                                                   !12, ~40
   31    62        IS_EQUAL                                                 !3, 1
         63      > JMPZ                                                     ~42, ->69
   33    64    >   IS_EQUAL                                                 !9, 0
         65      > JMPZ                                                     ~43, ->67
         66    >   ASSIGN_OP                                     8          !11, '%3Cspan+class%3D%22fc-red%22%3E'
   34    67    >   ASSIGN                                                   !9, 1
         68      > JMP                                                      ->75
   37    69    >   IS_EQUAL                                                 !9, 1
         70      > JMPZ                                                     ~46, ->75
   38    71    >   SUB                                              ~47     !12, 1
         72        ASSIGN                                                   !10, ~47
   39    73        ASSIGN                                                   !9, 0
   40    74        ASSIGN_OP                                     8          !11, '%3C%2Fspan%3E'
   44    75    >   INIT_FCALL                                               'mb_substr'
         76        SEND_VAR                                                 !0
         77        SEND_VAR                                                 !12
         78        SEND_VAL                                                 1
         79        DO_ICALL                                         $51     
         80        ASSIGN_OP                                     8          !11, $51
   30    81      > JMP                                                      ->60
         82    >   FE_FREE                                                  $39
   46    83      > RETURN                                                   !11
   47    84*     > RETURN                                                   null

End of function setkeywords

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.26 ms | 1407 KiB | 26 Q