I want to remove new lines from some html (with php) except in
tags where whitespace is obviously important.
It may be 3 years later, but... The following code will remove all line breaks and whitespace at long as it is outside of pre tags. Cheers!
function sanitize_output($buffer)
{
$search = array(
'/\>[^\S ]+/s', //strip whitespaces after tags, except space
'/[^\S ]+\',
'<',
'\\1'
);
$blocks = preg_split('/(<\/?pre[^>]*>)/', $buffer, null, PREG_SPLIT_DELIM_CAPTURE);
$buffer = '';
foreach($blocks as $i => $block)
{
if($i % 4 == 2)
$buffer .= $block; //break out ...
with \n's
else
$buffer .= preg_replace($search, $replace, $block);
}
return $buffer;
}
ob_start("sanitize_output");