I have the string $var in which I need to replace some text. The first \"X\" needs to be replaced by \"A\", the second \"X\" needs to be replaced by B and so on, here is an exam
Lots of loops are happening in the other answers, here's an alternative.
$var = 'X X X X X X X';
$replace = array('A', 'B', 'C', 'D');
$var = preg_replace(array_fill(0, count($replace), '/X/'), $replace, $var, 1);
echo $var; // A B C D X X X