Forum Moderators: coopster
However Ive seen files (created by others) that are when uploading it to the server, its "compact" (no spaces, linefeeds etc) obviously to save disk space.
How can I "compact" my source code, any software that I could use to do this?
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.
The "PHP binary" is the actual executable program that processes PHP scripts. When you upload a PHP script to your web server and then browse to that script, the web server loads the PHP binary in the background in order to turn your script into HTML.
If you installed PHP for Windows, even though it would normally be used by a web server, you would still be able to go to the command prompt and type "php" and the PHP program would start, just like any other program. You can then use its command line options, such as the "-w" as described above to strip white space from a PHP source file. See the following page for more information:
[uk2.php.net...]
[edited by: PHP_Chimp at 11:32 am (utc) on Nov. 3, 2007]
<edit>
My code was partly inspired by code that londrum posted here [webmasterworld.com], so it is well worth looking over. There (sorry dont know londrum is male or female:) ) code is very good so well worth a look if you are thinking about compacting pages.
</edit>
[edited by: PHP_Chimp at 2:39 pm (utc) on Nov. 3, 2007]