- preg_replace_callback: documentation ( source)
<?php
$myArray = ['yellow', 'green', 'red'];
$myString = 'banana is ?, apple is ?, tomato is ?';
echo preg_replace_callback(
'/\?/',
function() use ($myArray) {
static $i = 0;
return $myArray[$i++];
},
$myString
);