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

End of function setkeywords

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.15 ms | 1407 KiB | 26 Q