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 { $table = $column ? $table[$column] : $table; return array_map('self::regex_replace', $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); foreach($content as $key => $v){ $columns[$key] = $engine->apply($content, $key); } var_dump($columns);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Use of "self" in callables is deprecated in /in/Pk2rC on line 52 Deprecated: Use of "self" in callables is deprecated in /in/Pk2rC on line 52 Deprecated: Use of "self" in callables is deprecated in /in/Pk2rC on line 52 array(3) { [1]=> array(3) { [0]=> array(1) { ["row"]=> string(18) "My name is Oliver " } [1]=> array(1) { ["row"]=> string(16) "Can you see me? " } [2]=> array(1) { ["row"]=> string(1) " " } } [2]=> array(3) { [0]=> array(1) { ["row"]=> string(28) "Hey there! Some other text. " } [1]=> array(1) { ["row"]=> string(19) "What is up Oliver? " } [2]=> array(1) { ["row"]=> string(1) " " } } [3]=> array(2) { [0]=> array(1) { ["row"]=> string(41) "Some text on the third column. First row." } [1]=> array(1) { ["row"]=> string(1) " " } } }
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
array(3) { [1]=> array(3) { [0]=> array(1) { ["row"]=> string(18) "My name is Oliver " } [1]=> array(1) { ["row"]=> string(16) "Can you see me? " } [2]=> array(1) { ["row"]=> string(1) " " } } [2]=> array(3) { [0]=> array(1) { ["row"]=> string(28) "Hey there! Some other text. " } [1]=> array(1) { ["row"]=> string(19) "What is up Oliver? " } [2]=> array(1) { ["row"]=> string(1) " " } } [3]=> array(2) { [0]=> array(1) { ["row"]=> string(41) "Some text on the third column. First row." } [1]=> array(1) { ["row"]=> string(1) " " } } }

preferences:
157.82 ms | 404 KiB | 211 Q