3v4l.org

run code in 300+ PHP versions simultaneously
<?php $originalString = "Hello world, what do you think of today's weather"; $findString = ' '; $nthOccurrence = 8; $newWord = ' beautiful '; function replaceNth(string $input, string $find, string $replacement, int $nth = 1): string { $pattern = '/(?:.*?\K' . preg_quote($find, '/') . '){' . $nth . '}/'; return preg_replace($pattern, $replacement, $input, 1); } echo replaceNth($originalString, $findString, $newWord, $nthOccurrence);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Sukq
function name:  (null)
number of ops:  12
compiled vars:  !0 = $originalString, !1 = $findString, !2 = $nthOccurrence, !3 = $newWord
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 'Hello+world%2C+what+do+you+think+of+today%27s+weather'
    4     1        ASSIGN                                                   !1, '+'
    5     2        ASSIGN                                                   !2, 8
    6     3        ASSIGN                                                   !3, '+beautiful+'
   13     4        INIT_FCALL                                               'replacenth'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        SEND_VAR                                                 !3
          8        SEND_VAR                                                 !2
          9        DO_FCALL                                      0  $8      
         10        ECHO                                                     $8
         11      > RETURN                                                   1

Function replacenth:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Sukq
function name:  replaceNth
number of ops:  23
compiled vars:  !0 = $input, !1 = $find, !2 = $replacement, !3 = $nth, !4 = $pattern
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      1
    9     4        INIT_FCALL                                               'preg_quote'
          5        SEND_VAR                                                 !1
          6        SEND_VAL                                                 '%2F'
          7        DO_ICALL                                         $5      
          8        CONCAT                                           ~6      '%2F%28%3F%3A.%2A%3F%5CK', $5
          9        CONCAT                                           ~7      ~6, '%29%7B'
         10        CONCAT                                           ~8      ~7, !3
         11        CONCAT                                           ~9      ~8, '%7D%2F'
         12        ASSIGN                                                   !4, ~9
   10    13        INIT_FCALL                                               'preg_replace'
         14        SEND_VAR                                                 !4
         15        SEND_VAR                                                 !2
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 1
         18        DO_ICALL                                         $11     
         19        VERIFY_RETURN_TYPE                                       $11
         20      > RETURN                                                   $11
   11    21*       VERIFY_RETURN_TYPE                                       
         22*     > RETURN                                                   null

End of function replacenth

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.18 ms | 1006 KiB | 16 Q