- preg_replace_callback: documentation ( source)
<?php
$data = [
0 => 'john',
1 => 'robinson',
2 => '27-08-1980',
3 => 'football',
4 => 'pizza',
];
$template = "{0} {1} birthday {2} <br /> Hobbie : {3} <br /> favorite : {4}";
$string = preg_replace_callback(
'/\{(\d+)\}/',
function ($matches) use ($data) {
return $data[$matches[1]];
},
$template
);
echo $string;