<?php
$pattern = <<<REGEX
/
(?|
<a .+?>(*SKIP)(*FAIL)
|
@([a-z]+)
|
!([a-z]+(?:_[a-z]+)*)
|
%([a-z]+(?:-[a-z]+)*)
|
\{([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:
52.17 ms | 402 KiB | 62 Q