3v4l.org

run code in 300+ PHP versions simultaneously
<?php // all keys must be lowercase $keywords_external_path = array( "sweet potato" => "food/sweet-potato", "potato salads" => "food/potato-salads", "potato" => "food/potato", ); $data = array( 'post_content' => 'Sweet potato some text then potato then more text and then potato salads' ); $base = 'http://domain.com/'; // only if this is useful $keywords_external = array_keys($keywords_external_path); krsort($keywords_external); // to quickly discards useless position (if you have many keywords): you can also do the same with the second letter $keywords_first_letter = implode('', array_unique(array_reduce($keywords_external, function ($c, $i) { $c[]=$i[0]; return $c; }, []))); $pattern = '~\b(?=['. $keywords_first_letter . '])(?:' . implode('|', $keywords_external) . ')\b~i'; $result = preg_replace_callback($pattern, function ($m) use ($keywords_external_path, $base) { return '<a href="' . $base . $keywords_external_path . '">' . $m[0] . '</a>'; }, $data['post_content']); echo $result;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Ye8ec
function name:  (null)
number of ops:  45
compiled vars:  !0 = $keywords_external_path, !1 = $data, !2 = $base, !3 = $keywords_external, !4 = $keywords_first_letter, !5 = $pattern, !6 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    9     1        ASSIGN                                                   !1, <array>
   14     2        ASSIGN                                                   !2, 'http%3A%2F%2Fdomain.com%2F'
   16     3        INIT_FCALL                                               'array_keys'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $10     
          6        ASSIGN                                                   !3, $10
   17     7        INIT_FCALL                                               'krsort'
          8        SEND_REF                                                 !3
          9        DO_ICALL                                                 
   20    10        INIT_FCALL                                               'implode'
         11        SEND_VAL                                                 ''
         12        INIT_FCALL                                               'array_unique'
         13        INIT_FCALL                                               'array_reduce'
         14        SEND_VAR                                                 !3
         15        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYe8ec%3A20%240'
         16        SEND_VAL                                                 ~13
         17        SEND_VAL                                                 <array>
         18        DO_ICALL                                         $14     
         19        SEND_VAR                                                 $14
         20        DO_ICALL                                         $15     
         21        SEND_VAR                                                 $15
         22        DO_ICALL                                         $16     
         23        ASSIGN                                                   !4, $16
   22    24        CONCAT                                           ~18     '%7E%5Cb%28%3F%3D%5B', !4
         25        CONCAT                                           ~19     ~18, '%5D%29%28%3F%3A'
         26        INIT_FCALL                                               'implode'
         27        SEND_VAL                                                 '%7C'
         28        SEND_VAR                                                 !3
         29        DO_ICALL                                         $20     
         30        CONCAT                                           ~21     ~19, $20
         31        CONCAT                                           ~22     ~21, '%29%5Cb%7Ei'
         32        ASSIGN                                                   !5, ~22
   24    33        INIT_FCALL                                               'preg_replace_callback'
         34        SEND_VAR                                                 !5
         35        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYe8ec%3A24%241'
         36        BIND_LEXICAL                                             ~24, !0
         37        BIND_LEXICAL                                             ~24, !2
   26    38        SEND_VAL                                                 ~24
         39        FETCH_DIM_R                                      ~25     !1, 'post_content'
         40        SEND_VAL                                                 ~25
         41        DO_ICALL                                         $26     
   24    42        ASSIGN                                                   !6, $26
   28    43        ECHO                                                     !6
         44      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FYe8ec%3A20%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Ye8ec
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $c, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        FETCH_DIM_R                                      ~3      !1, 0
          3        ASSIGN_DIM                                               !0
          4        OP_DATA                                                  ~3
          5      > RETURN                                                   !0
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYe8ec%3A20%240

Function %00%7Bclosure%7D%2Fin%2FYe8ec%3A24%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Ye8ec
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $m, !1 = $keywords_external_path, !2 = $base
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   25     3        CONCAT                                           ~3      '%3Ca+href%3D%22', !2
          4        CONCAT                                           ~4      ~3, !1
          5        CONCAT                                           ~5      ~4, '%22%3E'
          6        FETCH_DIM_R                                      ~6      !0, 0
          7        CONCAT                                           ~7      ~5, ~6
          8        CONCAT                                           ~8      ~7, '%3C%2Fa%3E'
          9      > RETURN                                                   ~8
   26    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYe8ec%3A24%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.3 ms | 1400 KiB | 25 Q