3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = 'try replacing {foo} and {huh} and {bar} in text'; $data_array = [ 'foo' => 'fighters', 'bar' => 'snacks', ]; echo preg_replace_callback( '/{(\w+)}/', fn($m) => $data_array[$m[1]] ?? $m[0], $string );

preferences:
68.27 ms | 404 KiB | 5 Q