you can use preg_replace_callback with anonymous functions
<?php $c['GOOD']='very good'; $c['BOY']='jimmy'; $str="hello c(GOOD) world c(BOY) "; echo preg_replace_callback('@c\((.*?)\)@', function ($match) use ($c) { if (isset($c[$match[1]])) return $c[$match[1]]; }, $str); ?>
The advantage of this function is this: it allows you to pass the second parameter, see use ($c) . Then there is no need to create a second function.
user1646111
source share