3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = 'The ID is \$ID and name is \$name'; $row = [ 'ID'=> 5, 'name' => 'delboy1978uk', ]; function replaceStuff($string, $row) { preg_match_all('#\\$\w+#', $string, $matches); foreach ($matches[0] as $match) { $key = str_replace('$', '', $match); $replace = '\\'.$match; $string = str_replace($replace, $row[$key], $string); } return $string; } echo replaceStuff($string, $row);

preferences:
43.93 ms | 1703 KiB | 5 Q