3v4l.org

run code in 300+ PHP versions simultaneously
<?php $pattern = <<<REGEX / (?| <a .+?>(*SKIP)(*FAIL) | @([a-z]+) | !(\w+(?:_\w+)*) | %(\w+(?:-\w+)*) | \{([a-z]+)}(\.[a-z]+)? ) /sx REGEX; $lookup = [ 'foo' => 'oof', 'bar' => 'sand', 'foobar' => 'FUBAR', 'foo.bar' => 'nuts', 'bar_baz' => 'boo', 'foo-baz' => 'tubas', ]; $input = 'Hello Bob @foo <a href="/someurl/!foobar/!bar/word"> Word {foobar} </a> %foo someword !bar_baz'; echo preg_replace_callback( $pattern, fn($m) => $lookup[implode(array_slice($m, 1))] ?? $m[0], $input );
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Hello Bob oof <a href="/someurl/!foobar/!bar/word"> Word FUBAR </a> oof someword boo

preferences:
54.4 ms | 402 KiB | 62 Q