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

End of function setkeywords

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.55 ms | 1407 KiB | 28 Q