- strpos: documentation ( source)
- substr_replace: documentation ( source)
<?php
$myArray = ['yellow', 'green'];
$myString = 'banana is ?, apple is ?, lemon is ?, tomato is ?';
$count = count($myArray);
$i = 0;
while (($pos = strpos($myString, '?')) !== false) {
$myString = substr_replace(
$myString,
$myArray[$i++ % $count],
$pos,
1
);
}
echo $myString;