I find it odd to have huge HTML strings, then using some string search and replace hack to format that afterwards...
When constructing HTML with PHP, I like using arrays :
$htmlArr = array();
foreach ($dataSet as $index => $data) {
$htmlArr[] = '<p>Line#'.$index.' : <span>' . $data . '</span></p>';
}
$html = implode("\n", $htmlArr);
This way, every HTML line has it's separate $htmlArr[] value. Moreover, if you need your HTML to be "pretty print", you can simply have some sort of method that will indent your HTML by prepending whitespaces at the beginning of every array elements depending on somme rule set. For example, if we have :
$htmlArr = array(
'<ol>',
'<li>Item 1</li>',
'<li><a href="#">Item 2</a></li>',
'<li>Item 3</li>',
'</ol>'
);
Then the formatting function algorithm would be (very simple one, considering that the HTML is well constructed) :
$indent = 0; // initial indent
foreach & $value in $array
$open = count how many opened elements
$closed = count how many closed elements
$value = str_repeat(' ', $indent * TAB_SPACE) . $value;
$indent += $open - $closed; // next line's indent
end foreach
return $array
Then implode("\n", $array)
for the prettyfied HTML
** UPDATE **
After the question edit by Felix Kling, I realize that this has nothing to do with the question. Sorry about that :) Thanks though for the clarification.