Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- Compacting PHP files


londrum - 9:41 pm on Oct 31, 2007 (gmt 0)


try wrapping this around your code...

at the very top of your page, above everything else, put

<?php
ob_start('ob_gzhandler');
ob_start('compress');
function compress($buffer)
{
$buffer=preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','',$buffer);
$buffer=str_replace(array("\r\n","\r","\n","\t"),'',$buffer);
return $buffer;
}
?>

and at the very bottom

<?php
ob_end_flush();
ob_end_flush();
?>

when you view the source code in your browser it should all be crunched up - with the comments stripped out. it will be gzipped up to.

you'll probably have to add a few expressions to it though to catch a few more, because everyone's pages are different so i've only put the usual ones in.
you have to be careful with in-page javascript though - make sure the lines all end properly because when it removes the line breaks it might break the script

[edit] i should also mention that it's not a great idea to do this every time that somebody views the page. the best thing to do is do it once and then cache it, and send the cached version to the user.


Thread source:: http://www.webmasterworld.com/php/3492779.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com