3v4l.org

run code in 500+ PHP versions simultaneously
<?php $format='<number_from_format>{{number}}</number_from_format>{{prefix}}'; // Here {{prefix}} is first and {{number}} is last: $replacements = [ '{{prefix}}' => "<number_from_prefix>{{number}}</number_from_prefix>", '{{number}}' => 123, ]; $old=str_replace(array_keys($replacements), array_values($replacements), $format); $new=strtr($format,$replacements); var_dump($old,$new); // Now flip it, {{prefix}} last and {{number}} first: $format='<number_from_format>{{number}}</number_from_format>{{prefix}}'; $replacements = [ '{{number}}' => 123, '{{prefix}}' => "<number_from_prefix>{{number}}</number_from_prefix>", ]; $old=str_replace(array_keys($replacements), array_values($replacements), $format); $new=strtr($format,$replacements); var_dump($new,$old); // now lets do that with strtr_recursive: function strtr_recursive(string $str, array $table, int $maxDepth = 100): string { $depth = 0; while ($depth < $maxDepth) { $old = $str; $str = strtr($str, $table); if ($old === $str) { return $str; } ++$depth; } throw new \RuntimeException("translation exceeded max recursion depth ($maxDepth)"); } // with strtr_recursive, it does not matter if {{number}} or {{prefix}} comes first. $format='<number_from_format>{{number}}</number_from_format>{{prefix}}'; $replacements = [ '{{number}}' => 123, '{{prefix}}' => "<number_from_prefix>{{number}}</number_from_prefix>", ]; $old=str_replace(array_keys($replacements), array_values($replacements), $format); $new=strtr_recursive($format,$replacements); var_dump($old,$new);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XlWDE
function name:  (null)
number of ops:  55
compiled vars:  !0 = $format, !1 = $replacements, !2 = $old, !3 = $new
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                       !0, '%3Cnumber_from_format%3E%7B%7Bnumber%7D%7D%3C%2Fnumber_from_format%3E%7B%7Bprefix%7D%7D'
    4     1        ASSIGN                                                       !1, <array>
    8     2        INIT_FCALL                                                   'array_keys'
          3        SEND_VAR                                                     !1
          4        DO_ICALL                                             $6      
          5        INIT_FCALL                                                   'array_values'
          6        SEND_VAR                                                     !1
          7        DO_ICALL                                             $7      
          8        FRAMELESS_ICALL_3                str_replace         ~8      $6, $7
          9        OP_DATA                                                      !0
         10        ASSIGN                                                       !2, ~8
    9    11        FRAMELESS_ICALL_2                strtr               ~10     !0, !1
         12        ASSIGN                                                       !3, ~10
   10    13        INIT_FCALL                                                   'var_dump'
         14        SEND_VAR                                                     !2
         15        SEND_VAR                                                     !3
         16        DO_ICALL                                                     
   12    17        ASSIGN                                                       !0, '%3Cnumber_from_format%3E%7B%7Bnumber%7D%7D%3C%2Fnumber_from_format%3E%7B%7Bprefix%7D%7D'
   13    18        ASSIGN                                                       !1, <array>
   17    19        INIT_FCALL                                                   'array_keys'
         20        SEND_VAR                                                     !1
         21        DO_ICALL                                             $15     
         22        INIT_FCALL                                                   'array_values'
         23        SEND_VAR                                                     !1
         24        DO_ICALL                                             $16     
         25        FRAMELESS_ICALL_3                str_replace         ~17     $15, $16
         26        OP_DATA                                                      !0
         27        ASSIGN                                                       !2, ~17
   18    28        FRAMELESS_ICALL_2                strtr               ~19     !0, !1
         29        ASSIGN                                                       !3, ~19
   19    30        INIT_FCALL                                                   'var_dump'
         31        SEND_VAR                                                     !3
         32        SEND_VAR                                                     !2
         33        DO_ICALL                                                     
   36    34        ASSIGN                                                       !0, '%3Cnumber_from_format%3E%7B%7Bnumber%7D%7D%3C%2Fnumber_from_format%3E%7B%7Bprefix%7D%7D'
   37    35        ASSIGN                                                       !1, <array>
   41    36        INIT_FCALL                                                   'array_keys'
         37        SEND_VAR                                                     !1
         38        DO_ICALL                                             $24     
         39        INIT_FCALL                                                   'array_values'
         40        SEND_VAR                                                     !1
         41        DO_ICALL                                             $25     
         42        FRAMELESS_ICALL_3                str_replace         ~26     $24, $25
         43        OP_DATA                                                      !0
         44        ASSIGN                                                       !2, ~26
   42    45        INIT_FCALL                                                   'strtr_recursive'
         46        SEND_VAR                                                     !0
         47        SEND_VAR                                                     !1
         48        DO_FCALL                                          0  $28     
         49        ASSIGN                                                       !3, $28
   43    50        INIT_FCALL                                                   'var_dump'
         51        SEND_VAR                                                     !2
         52        SEND_VAR                                                     !3
         53        DO_ICALL                                                     
         54      > RETURN                                                       1

Function strtr_recursive:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 5
Branch analysis from position: 15
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 5
Branch analysis from position: 15
Branch analysis from position: 5
filename:       /in/XlWDE
function name:  strtr_recursive
number of ops:  24
compiled vars:  !0 = $str, !1 = $table, !2 = $maxDepth, !3 = $depth, !4 = $old
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   21     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      100
   23     3        ASSIGN                                                       !3, 0
   24     4      > JMP                                                          ->13
   25     5    >   ASSIGN                                                       !4, !0
   26     6        FRAMELESS_ICALL_2                strtr               ~7      !0, !1
          7        ASSIGN                                                       !0, ~7
   28     8        IS_IDENTICAL                                                 !4, !0
          9      > JMPZ                                                         ~9, ->12
   29    10    >   VERIFY_RETURN_TYPE                                           !0
         11      > RETURN                                                       !0
   31    12    >   PRE_INC                                                      !3
   24    13    >   IS_SMALLER                                                   !3, !2
         14      > JMPNZ                                                        ~11, ->5
   33    15    >   NEW                                                  $12     'RuntimeException'
         16        ROPE_INIT                                         3  ~14     'translation+exceeded+max+recursion+depth+%28'
         17        ROPE_ADD                                          1  ~14     ~14, !2
         18        ROPE_END                                          2  ~13     ~14, '%29'
         19        SEND_VAL_EX                                                  ~13
         20        DO_FCALL                                          0          
         21      > THROW                                             0          $12
   34    22*       VERIFY_RETURN_TYPE                                           
         23*     > RETURN                                                       null

End of function strtr_recursive

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
129.32 ms | 492 KiB | 11 Q