I thought this was a nice function until I realised it wouldnt strip down html. As i'd been reading an article on compressing output to speed up delivery.
So I wrote a little one to do that for me. Here its is, incase people were looking for a html version. It may need tweaking, like with existing 's.
<?php
function strip_html($data)
{
$data=preg_replace_callback("/>[^<]*<\\/textarea/i", "harden_characters", $data);
$data=preg_replace_callback("/\\"[^"<>]+\\"/", "harden_characters", $data);
$data=preg_replace("/(//.*n)/","",$data); // remove single line comments, like this, from // to \\n
$data=preg_replace("/(t|r|n)/","",$data); // remove new lines \\n, tabs and \\r
$data=preg_replace("/(/*.**/)/","",$data); // remove multi-line comments /* */
$data=preg_replace("/(<![^>]*>)/","",$data); // remove multi-line comments <!-- -->
$data=preg_replace('/(\\s+)/', ' ',$data); // replace multi spaces with singles
$data=preg_replace('/>\\s</', '><',$data);
$data=preg_replace_callback("/"[^\\"<>]+"/", "unharden_characters", $data);
$data=preg_replace_callback("/>[^<]*<\\/textarea/", "unharden_characters", $data);
return $data;
}
function harden_characters($array)
{
$safe=$array[0];
$safe=preg_replace('/\\n/', "%0A", $safe);
$safe=preg_replace('/\\t/', "%09", $safe);
$safe=preg_replace('/\\s/', " ", $safe);
return $safe;
}
function unharden_characters($array)
{
$safe=$array[0];
$safe=preg_replace('/%0A/', "\\n", $safe);
$safe=preg_replace('/%09/', "\\t", $safe);
$safe=preg_replace('/ /', " ", $safe);
return $safe;
}
?>
The article code was similar to this, which shouldn't work as php_strip_whitespace takes a filename as input:-
<?php
$data=ob_get_contents();
ob_end_clean();
if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip'))
{
$data=gzencode(php_strip_whitespace($data),9);
header('Content-Encoding: gzip');
}
echo $data;
?>