3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '{ "1": [ { "row": "My name is Trevor\n" }, { "row": "Can you see me?\n" }, { "row": "\f" } ], "2": [ { "row": "Hey there! Some other text.\n" }, { "row": "What is up Trevor?\n" }, { "row": "\f" } ], "3": [ { "row": "Some text on the third column. First row." }, { "row": "\f" } ] }'; $content = json_decode($json, true); class regexTextReplace { private $pattern; private $replacement; public function __construct(array $arguments) { $this->pattern = $arguments['pattern']; $this->replacement = $arguments['replacement']; } public function apply(array $table, $column = false): array { $out = array(); foreach ($table as $col => $rows) { if ($column === false || $col == $column) { $out[$col] = array_map('self::regex_replace', $rows); } else { $out[$col] = $rows; } } return $out; } public function regex_replace(array $table) { return preg_replace($this->pattern, $this->replacement, $table); } } $options = [ 'pattern' => '/Trevor/i', 'replacement' => 'Oliver', ]; $engine = new regexTextReplace($options); $columns = $engine->apply($content, 1); print_r($columns); $options = [ 'pattern' => '/Some/i', 'replacement' => 'A lot of', ]; $engine = new regexTextReplace($options); $columns = $engine->apply($content); print_r($columns);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/P7NCu
function name:  (null)
number of ops:  32
compiled vars:  !0 = $json, !1 = $content, !2 = $options, !3 = $engine, !4 = $columns
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, '%7B%0A++++%221%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22My+name+is+Trevor%5Cn%22%0A++++++++%7D%2C%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22Can+you+see+me%3F%5Cn%22%0A++++++++%7D%2C%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22%5Cf%22%0A++++++++%7D%0A++++%5D%2C%0A++++%222%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22Hey+there%21+Some+other+text.%5Cn%22%0A++++++++%7D%2C%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22What+is+up+Trevor%3F%5Cn%22%0A++++++++%7D%2C%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22%5Cf%22%0A++++++++%7D%0A++++%5D%2C%0A++++%223%22%3A+%5B%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22Some+text+on+the+third+column.+First+row.%22%0A++++++++%7D%2C%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22%5Cf%22%0A++++++++%7D%0A++++%5D%0A%7D'
   35     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !1, $6
   68     6        ASSIGN                                                   !2, <array>
   72     7        NEW                                              $9      'regexTextReplace'
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !3, $9
   73    11        INIT_METHOD_CALL                                         !3, 'apply'
         12        SEND_VAR_EX                                              !1
         13        SEND_VAL_EX                                              1
         14        DO_FCALL                                      0  $12     
         15        ASSIGN                                                   !4, $12
   74    16        INIT_FCALL                                               'print_r'
         17        SEND_VAR                                                 !4
         18        DO_ICALL                                                 
   76    19        ASSIGN                                                   !2, <array>
   80    20        NEW                                              $16     'regexTextReplace'
         21        SEND_VAR_EX                                              !2
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !3, $16
   81    24        INIT_METHOD_CALL                                         !3, 'apply'
         25        SEND_VAR_EX                                              !1
         26        DO_FCALL                                      0  $19     
         27        ASSIGN                                                   !4, $19
   82    28        INIT_FCALL                                               'print_r'
         29        SEND_VAR                                                 !4
         30        DO_ICALL                                                 
         31      > RETURN                                                   1

Class regexTextReplace:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/P7NCu
function name:  __construct
number of ops:  8
compiled vars:  !0 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   44     1        FETCH_DIM_R                                      ~2      !0, 'pattern'
          2        ASSIGN_OBJ                                               'pattern'
          3        OP_DATA                                                  ~2
   45     4        FETCH_DIM_R                                      ~4      !0, 'replacement'
          5        ASSIGN_OBJ                                               'replacement'
          6        OP_DATA                                                  ~4
   46     7      > RETURN                                                   null

End of function __construct

Function apply:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 21
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 21
Branch analysis from position: 5
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 18
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/P7NCu
function name:  apply
number of ops:  26
compiled vars:  !0 = $table, !1 = $column, !2 = $out, !3 = $rows, !4 = $col
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   50     2        ASSIGN                                                   !2, <array>
   51     3      > FE_RESET_R                                       $6      !0, ->21
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->21
          5    >   ASSIGN                                                   !4, ~7
   52     6        TYPE_CHECK                                    4  ~9      !1
          7      > JMPNZ_EX                                         ~9      ~9, ->10
          8    >   IS_EQUAL                                         ~10     !4, !1
          9        BOOL                                             ~9      ~10
         10    > > JMPZ                                                     ~9, ->18
   53    11    >   INIT_FCALL                                               'array_map'
         12        SEND_VAL                                                 'self%3A%3Aregex_replace'
         13        SEND_VAR                                                 !3
         14        DO_ICALL                                         $12     
         15        ASSIGN_DIM                                               !2, !4
         16        OP_DATA                                                  $12
         17      > JMP                                                      ->20
   56    18    >   ASSIGN_DIM                                               !2, !4
         19        OP_DATA                                                  !3
   51    20    > > JMP                                                      ->4
         21    >   FE_FREE                                                  $6
   59    22        VERIFY_RETURN_TYPE                                       !2
         23      > RETURN                                                   !2
   60    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function apply

Function regex_replace:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/P7NCu
function name:  regex_replace
number of ops:  10
compiled vars:  !0 = $table
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
   64     1        INIT_FCALL                                               'preg_replace'
          2        FETCH_OBJ_R                                      ~1      'pattern'
          3        SEND_VAL                                                 ~1
          4        FETCH_OBJ_R                                      ~2      'replacement'
          5        SEND_VAL                                                 ~2
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $3      
          8      > RETURN                                                   $3
   65     9*     > RETURN                                                   null

End of function regex_replace

End of class regexTextReplace.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.34 ms | 1404 KiB | 21 Q