- preg_replace_callback: documentation ( source)
<?php
$text = "Line <br> Line <br> Line <br> Line <br> Line <br> Line <br>";
$replace = array("1", "2", "3", "4");
$counter = 0;
$text = preg_replace_callback('~<br\s*/?>~', function($m) use($replace, &$counter) {
if ($counter === count($replace)) { $counter = 0; }
return $replace[$counter++];
}, $text);
echo $text;