3v4l.org

run code in 300+ PHP versions simultaneously
<?php $document = json_decode(<<<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?\\n" }, { "row": "\\f" } ], "3": [ { "row": "Some text on the third column. First row. Trevor btw." }, { "row": "\\f" } ] } 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, $columns = false): array { $columns = $columns === false ? array_keys($table) : (array)$columns; return array_map(function ($column) use ($table, $columns) { return in_array($column, $columns) ? array_map('self::regex_replace', $table[$column]) : $table[$column]; }, array_keys($table)); } public function regex_replace(array $table) { return preg_replace($this->pattern, $this->replacement, $table); } } $options = [ 'pattern' => '/Trevor/i', 'replacement' => 'Oliver', ]; $engine = new regexTextReplace($options); print_r('======= ONLY COLUMN 1 =======' . PHP_EOL . PHP_EOL); print_r($engine->apply($document, 1)); print_r(PHP_EOL . PHP_EOL . '======= COLUMNS 1 & 3 =======' . PHP_EOL . PHP_EOL); print_r($engine->apply($document, [1, 3])); print_r(PHP_EOL . PHP_EOL . '======= ALL TABLE =======' . PHP_EOL . PHP_EOL); print_r($engine->apply($document));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kn4FY
function name:  (null)
number of ops:  40
compiled vars:  !0 = $document, !1 = $options, !2 = $engine
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'json_decode'
    3     1        SEND_VAL                                                 '%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%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.+Trevor+btw.%22%0A++++++++%7D%2C%0A++++++++%7B%0A++++++++++++%22row%22%3A+%22%5Cf%22%0A++++++++%7D%0A++++%5D%0A%7D'
   36     2        SEND_VAL                                                 <true>
          3        DO_ICALL                                         $3      
    2     4        ASSIGN                                                   !0, $3
   65     5        ASSIGN                                                   !1, <array>
   69     6        NEW                                              $6      'regexTextReplace'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !2, $6
   71    10        INIT_FCALL                                               'print_r'
         11        SEND_VAL                                                 '%3D%3D%3D%3D%3D%3D%3D+ONLY+COLUMN+1+%3D%3D%3D%3D%3D%3D%3D%0A%0A'
         12        DO_ICALL                                                 
   72    13        INIT_FCALL                                               'print_r'
         14        INIT_METHOD_CALL                                         !2, 'apply'
         15        SEND_VAR_EX                                              !0
         16        SEND_VAL_EX                                              1
         17        DO_FCALL                                      0  $10     
         18        SEND_VAR                                                 $10
         19        DO_ICALL                                                 
   73    20        INIT_FCALL                                               'print_r'
         21        SEND_VAL                                                 '%0A%0A%3D%3D%3D%3D%3D%3D%3D+COLUMNS+1+%26+3+%3D%3D%3D%3D%3D%3D%3D%0A%0A'
         22        DO_ICALL                                                 
   74    23        INIT_FCALL                                               'print_r'
         24        INIT_METHOD_CALL                                         !2, 'apply'
         25        SEND_VAR_EX                                              !0
         26        SEND_VAL_EX                                              <array>
         27        DO_FCALL                                      0  $13     
         28        SEND_VAR                                                 $13
         29        DO_ICALL                                                 
   75    30        INIT_FCALL                                               'print_r'
         31        SEND_VAL                                                 '%0A%0A%3D%3D%3D%3D%3D%3D%3D+ALL+TABLE+%3D%3D%3D%3D%3D%3D%3D%0A%0A'
         32        DO_ICALL                                                 
   76    33        INIT_FCALL                                               'print_r'
         34        INIT_METHOD_CALL                                         !2, 'apply'
         35        SEND_VAR_EX                                              !0
         36        DO_FCALL                                      0  $16     
         37        SEND_VAR                                                 $16
         38        DO_ICALL                                                 
         39      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FKn4FY%3A54%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kn4FY
function name:  {closure}
number of ops:  19
compiled vars:  !0 = $column, !1 = $table, !2 = $columns
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   55     3        INIT_FCALL                                               'in_array'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !2
          6        DO_ICALL                                         $3      
          7      > JMPZ                                                     $3, ->15
          8    >   INIT_FCALL                                               'array_map'
          9        SEND_VAL                                                 'self%3A%3Aregex_replace'
         10        FETCH_DIM_R                                      ~4      !1, !0
         11        SEND_VAL                                                 ~4
         12        DO_ICALL                                         $5      
         13        QM_ASSIGN                                        ~6      $5
         14      > JMP                                                      ->17
         15    >   FETCH_DIM_R                                      ~7      !1, !0
         16        QM_ASSIGN                                        ~6      ~7
         17    > > RETURN                                                   ~6
   56    18*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FKn4FY%3A54%240

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

End of function __construct

Function apply:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Kn4FY
function name:  apply
number of ops:  26
compiled vars:  !0 = $table, !1 = $columns
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   52     2        TYPE_CHECK                                    4          !1
          3      > JMPZ                                                     ~2, ->9
          4    >   INIT_FCALL                                               'array_keys'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7        QM_ASSIGN                                        ~4      $3
          8      > JMP                                                      ->11
          9    >   CAST                                          7  ~5      !1
         10        QM_ASSIGN                                        ~4      ~5
         11    >   ASSIGN                                                   !1, ~4
   54    12        INIT_FCALL                                               'array_map'
         13        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FKn4FY%3A54%240'
         14        BIND_LEXICAL                                             ~7, !0
         15        BIND_LEXICAL                                             ~7, !1
   56    16        SEND_VAL                                                 ~7
         17        INIT_FCALL                                               'array_keys'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $8      
         20        SEND_VAR                                                 $8
         21        DO_ICALL                                         $9      
         22        VERIFY_RETURN_TYPE                                       $9
         23      > RETURN                                                   $9
   57    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/Kn4FY
function name:  regex_replace
number of ops:  10
compiled vars:  !0 = $table
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   61     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
   62     9*     > RETURN                                                   null

End of function regex_replace

End of class regexTextReplace.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.37 ms | 1409 KiB | 25 Q