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));
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
======= ONLY COLUMN 1 ======= Deprecated: Use of "self" in callables is deprecated in /in/Kn4FY on line 55 Array ( [0] => Array ( [0] => Array ( [row] => My name is Oliver ) [1] => Array ( [row] => Can you see me? ) [2] => Array ( [row] => ) ) [1] => Array ( [0] => Array ( [row] => Hey there! Some other text. ) [1] => Array ( [row] => What is up? ) [2] => Array ( [row] => ) ) [2] => Array ( [0] => Array ( [row] => Some text on the third column. First row. Trevor btw. ) [1] => Array ( [row] => ) ) ) ======= COLUMNS 1 & 3 ======= Deprecated: Use of "self" in callables is deprecated in /in/Kn4FY on line 55 Deprecated: Use of "self" in callables is deprecated in /in/Kn4FY on line 55 Array ( [0] => Array ( [0] => Array ( [row] => My name is Oliver ) [1] => Array ( [row] => Can you see me? ) [2] => Array ( [row] => ) ) [1] => Array ( [0] => Array ( [row] => Hey there! Some other text. ) [1] => Array ( [row] => What is up? ) [2] => Array ( [row] => ) ) [2] => Array ( [0] => Array ( [row] => Some text on the third column. First row. Oliver btw. ) [1] => Array ( [row] => ) ) ) ======= ALL TABLE ======= Deprecated: Use of "self" in callables is deprecated in /in/Kn4FY on line 55 Deprecated: Use of "self" in callables is deprecated in /in/Kn4FY on line 55 Deprecated: Use of "self" in callables is deprecated in /in/Kn4FY on line 55 Array ( [0] => Array ( [0] => Array ( [row] => My name is Oliver ) [1] => Array ( [row] => Can you see me? ) [2] => Array ( [row] => ) ) [1] => Array ( [0] => Array ( [row] => Hey there! Some other text. ) [1] => Array ( [row] => What is up? ) [2] => Array ( [row] => ) ) [2] => Array ( [0] => Array ( [row] => Some text on the third column. First row. Oliver btw. ) [1] => Array ( [row] => ) ) )
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
======= ONLY COLUMN 1 ======= Array ( [0] => Array ( [0] => Array ( [row] => My name is Oliver ) [1] => Array ( [row] => Can you see me? ) [2] => Array ( [row] => ) ) [1] => Array ( [0] => Array ( [row] => Hey there! Some other text. ) [1] => Array ( [row] => What is up? ) [2] => Array ( [row] => ) ) [2] => Array ( [0] => Array ( [row] => Some text on the third column. First row. Trevor btw. ) [1] => Array ( [row] => ) ) ) ======= COLUMNS 1 & 3 ======= Array ( [0] => Array ( [0] => Array ( [row] => My name is Oliver ) [1] => Array ( [row] => Can you see me? ) [2] => Array ( [row] => ) ) [1] => Array ( [0] => Array ( [row] => Hey there! Some other text. ) [1] => Array ( [row] => What is up? ) [2] => Array ( [row] => ) ) [2] => Array ( [0] => Array ( [row] => Some text on the third column. First row. Oliver btw. ) [1] => Array ( [row] => ) ) ) ======= ALL TABLE ======= Array ( [0] => Array ( [0] => Array ( [row] => My name is Oliver ) [1] => Array ( [row] => Can you see me? ) [2] => Array ( [row] => ) ) [1] => Array ( [0] => Array ( [row] => Hey there! Some other text. ) [1] => Array ( [row] => What is up? ) [2] => Array ( [row] => ) ) [2] => Array ( [0] => Array ( [row] => Some text on the third column. First row. Oliver btw. ) [1] => Array ( [row] => ) ) )

preferences:
149.46 ms | 410 KiB | 213 Q