- preg_replace_callback: documentation ( source)
- array_shift: documentation ( source)
<?php
$string = "I have some fruits like [name], [another_name] and [one_another_name]";
$fruits_array = array("Banana", "Apple", "Orange");
$string = preg_replace_callback(
'/\[[^]]+\]/',
function ($matches) use (&$fruits_array) {
return array_shift($fruits_array);
},
$string
);
echo $string;