3v4l.org

run code in 500+ PHP versions simultaneously
<?php $myVar = "My sister alannis Is not That blonde, here is a good place. I know Ariane is not MY SISTER!"; echo "$myVar\n"; $myWords = [ ["is", "é"], ["on", "no"], ["that", "aquela"], ["sister", "irmã"], ["my", "minha"], ["myth", "mito"], ["he", "ele"], ["good", "bom"], ["ace", "perito"], ["i", "eu"] // notice I must be lowercase ]; $translations = array_column($myWords, 1, 0); // or skip this step and just declare $myWords as key-value pairs // length sorting is not necessary // preg_quote() and \Q\E are not used because dealing with words only (no danger of misinterpretation by regex) $pattern = '/\b(?>' . implode('|', array_keys($translations)) . ')\b/i'; // atomic group is slightly faster (no backtracking) /* echo $pattern; makes: /\b(?>is|on|that|sister|my|myth|he|good|ace)\b/i demo: https://regex101.com/r/DXTtDf/1 */ $translated = preg_replace_callback( $pattern, function($m) use($translations) { // bring $translations (lookup) array to function $encoding = 'UTF-8'; // default setting $key = mb_strtolower($m[0], $encoding); // standardize keys' case for lookup accessibility if (ctype_lower($m[0])) { // treat as all lower return $translations[$m[0]]; } elseif (mb_strlen($m[0], $encoding) > 1 && ctype_upper($m[0])) { // treat as all uppercase return mb_strtoupper($translations[$key], $encoding); } else { // treat as only first character uppercase return mb_strtoupper(mb_substr($translations[$key], 0, 1, $encoding), $encoding) // uppercase first . mb_substr($translations[$key], 1, mb_strlen($translations[$key], $encoding) - 1, $encoding); // append remaining lowercase } }, $myVar ); echo $translated;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0I93E
function name:  (null)
number of ops:  28
compiled vars:  !0 = $myVar, !1 = $myWords, !2 = $translations, !3 = $pattern, !4 = $translated
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, 'My+sister+alannis+Is+not+That+blonde%2C+here+is+a+good+place.+I+know+Ariane+is+not+MY+SISTER%21'
    4     1        NOP                                                          
          2        FAST_CONCAT                                          ~6      !0, '%0A'
          3        ECHO                                                         ~6
    6     4        ASSIGN                                                       !1, <array>
   18     5        INIT_FCALL                                                   'array_column'
          6        SEND_VAR                                                     !1
          7        SEND_VAL                                                     1
          8        SEND_VAL                                                     0
          9        DO_ICALL                                             $8      
         10        ASSIGN                                                       !2, $8
   23    11        INIT_FCALL                                                   'array_keys'
         12        SEND_VAR                                                     !2
         13        DO_ICALL                                             $10     
         14        FRAMELESS_ICALL_2                implode             ~11     '%7C', $10
         15        CONCAT                                               ~12     '%2F%5Cb%28%3F%3E', ~11
         16        CONCAT                                               ~13     ~12, '%29%5Cb%2Fi'
         17        ASSIGN                                                       !3, ~13
   28    18        INIT_FCALL                                                   'preg_replace_callback'
   29    19        SEND_VAR                                                     !3
   30    20        DECLARE_LAMBDA_FUNCTION                              ~15     [0]
         21        BIND_LEXICAL                                                 ~15, !2
   41    22        SEND_VAL                                                     ~15
   42    23        SEND_VAR                                                     !0
   28    24        DO_ICALL                                             $16     
         25        ASSIGN                                                       !4, $16
   45    26        ECHO                                                         !4
   46    27      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 46) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 38
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/0I93E
function name:  {closure:/in/0I93E:30}
number of ops:  65
compiled vars:  !0 = $m, !1 = $translations, !2 = $encoding, !3 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   30     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
   31     2        ASSIGN                                                       !2, 'UTF-8'
   32     3        INIT_FCALL                                                   'mb_strtolower'
          4        FETCH_DIM_R                                          ~5      !0, 0
          5        SEND_VAL                                                     ~5
          6        SEND_VAR                                                     !2
          7        DO_ICALL                                             $6      
          8        ASSIGN                                                       !3, $6
   33     9        INIT_FCALL                                                   'ctype_lower'
         10        FETCH_DIM_R                                          ~8      !0, 0
         11        SEND_VAL                                                     ~8
         12        DO_ICALL                                             $9      
         13      > JMPZ                                                         $9, ->18
   34    14    >   FETCH_DIM_R                                          ~10     !0, 0
         15        FETCH_DIM_R                                          ~11     !1, ~10
         16      > RETURN                                                       ~11
   33    17*       JMP                                                          ->64
   35    18    >   INIT_FCALL                                                   'mb_strlen'
         19        FETCH_DIM_R                                          ~12     !0, 0
         20        SEND_VAL                                                     ~12
         21        SEND_VAR                                                     !2
         22        DO_ICALL                                             $13     
         23        IS_SMALLER                                           ~14     1, $13
         24      > JMPZ_EX                                              ~14     ~14, ->30
         25    >   INIT_FCALL                                                   'ctype_upper'
         26        FETCH_DIM_R                                          ~15     !0, 0
         27        SEND_VAL                                                     ~15
         28        DO_ICALL                                             $16     
         29        BOOL                                                 ~14     $16
         30    > > JMPZ                                                         ~14, ->38
   36    31    >   INIT_FCALL                                                   'mb_strtoupper'
         32        FETCH_DIM_R                                          ~17     !1, !3
         33        SEND_VAL                                                     ~17
         34        SEND_VAR                                                     !2
         35        DO_ICALL                                             $18     
         36      > RETURN                                                       $18
   35    37*       JMP                                                          ->64
   38    38    >   INIT_FCALL                                                   'mb_strtoupper'
         39        INIT_FCALL                                                   'mb_substr'
         40        FETCH_DIM_R                                          ~19     !1, !3
         41        SEND_VAL                                                     ~19
         42        SEND_VAL                                                     0
         43        SEND_VAL                                                     1
         44        SEND_VAR                                                     !2
         45        DO_ICALL                                             $20     
         46        SEND_VAR                                                     $20
         47        SEND_VAR                                                     !2
         48        DO_ICALL                                             $21     
   39    49        INIT_FCALL                                                   'mb_substr'
         50        FETCH_DIM_R                                          ~22     !1, !3
         51        SEND_VAL                                                     ~22
         52        SEND_VAL                                                     1
         53        INIT_FCALL                                                   'mb_strlen'
         54        FETCH_DIM_R                                          ~23     !1, !3
         55        SEND_VAL                                                     ~23
         56        SEND_VAR                                                     !2
         57        DO_ICALL                                             $24     
         58        SUB                                                  ~25     $24, 1
         59        SEND_VAL                                                     ~25
         60        SEND_VAR                                                     !2
         61        DO_ICALL                                             $26     
         62        CONCAT                                               ~27     $21, $26
         63      > RETURN                                                       ~27
   41    64*     > RETURN                                                       null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
187.46 ms | 2050 KiB | 22 Q