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; } var_dump($map); // 遍历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/YpfBl
function name:  (null)
number of ops:  8
compiled vars:  !0 = $title, !1 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   ASSIGN                                                   !0, '%E6%9C%8D%E5%8A%A1%E6%9C%8D%E5%8A%A1%E5%91%98'
   51     1        ASSIGN                                                   !1, <array>
   52     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 = 62, Position 2 = 84
Branch analysis from position: 62
2 jumps found. (Code = 78) Position 1 = 63, Position 2 = 84
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 71
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 68, Position 2 = 69
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 77
Branch analysis from position: 77
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
Branch analysis from position: 69
Branch analysis from position: 71
2 jumps found. (Code = 43) Position 1 = 73, Position 2 = 77
Branch analysis from position: 73
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
Branch analysis from position: 77
Branch analysis from position: 84
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 84
Branch analysis from position: 54
filename:       /in/YpfBl
function name:  setKeyWords
number of ops:  87
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
   26    55        INIT_FCALL                                               'var_dump'
         56        SEND_VAR                                                 !2
         57        DO_ICALL                                                 
   28    58        ASSIGN                                                   !9, 0
   29    59        ASSIGN                                                   !10, -1
   30    60        ASSIGN                                                   !11, ''
   31    61      > FE_RESET_R                                       $39     !2, ->84
         62    > > FE_FETCH_R                                       ~40     $39, !3, ->84
         63    >   ASSIGN                                                   !12, ~40
   32    64        IS_EQUAL                                                 !3, 1
         65      > JMPZ                                                     ~42, ->71
   34    66    >   IS_EQUAL                                                 !9, 0
         67      > JMPZ                                                     ~43, ->69
         68    >   ASSIGN_OP                                     8          !11, '%3Cspan+class%3D%22fc-red%22%3E'
   35    69    >   ASSIGN                                                   !9, 1
         70      > JMP                                                      ->77
   38    71    >   IS_EQUAL                                                 !9, 1
         72      > JMPZ                                                     ~46, ->77
   39    73    >   SUB                                              ~47     !12, 1
         74        ASSIGN                                                   !10, ~47
   40    75        ASSIGN                                                   !9, 0
   41    76        ASSIGN_OP                                     8          !11, '%3C%2Fspan%3E'
   45    77    >   INIT_FCALL                                               'mb_substr'
         78        SEND_VAR                                                 !0
         79        SEND_VAR                                                 !12
         80        SEND_VAL                                                 1
         81        DO_ICALL                                         $51     
         82        ASSIGN_OP                                     8          !11, $51
   31    83      > JMP                                                      ->62
         84    >   FE_FREE                                                  $39
   47    85      > RETURN                                                   !11
   48    86*     > RETURN                                                   null

End of function setkeywords

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
197.73 ms | 1411 KiB | 28 Q