Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Compacting PHP files



4:54 pm on Oct 31, 2007 (gmt 0)

10+ Year Member

Whenever I upload my files, I upload it as is (with spaces, linefeeds etc).

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?


4:57 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

look at this thread

no linefeeds causes issues


5:48 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

You can do it with the php binary itself using the -w (strip white space and comments) option. For example:

$php -w normal.php > compressed.php


5:53 pm on Oct 31, 2007 (gmt 0)

10+ Year Member

Sorry dmorison, I dont understand what is PHP Binary, im just using Notepad to write PHP, and upload it using an FTP software, please explain.


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

WebmasterWorld Senior Member 5+ Year Member

try wrapping this around your code...

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

function compress($buffer)
return $buffer;

and at the very bottom


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.


9:55 am on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


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:



11:31 am on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

function strip_buffer($bff, $code = null){
this section strips out tags which are not necessary for pages carrying an HTML 4.01 DOCTYPE -
including </li>, </td> and </tr>
if ($code == 'html'){
$bff = str_replace(' />','>',$bff);
$bff = str_replace('</li>','',$bff);
$bff = str_replace('</td>','',$bff);
$bff = str_replace('</tr>','',$bff);
$search = array('%>[^\S ]+%s', '%[^\S ]+<%s', '%(\s)+%s');
$replace = array('>', '<', '$1');
$bff = preg_replace($search, $replace, $bff);
$bff = preg_replace('%/\*[^*]*\*+([^/][^*]*\*+)*/%','',$bff);
return $bff;
This is a more aggressive function that londrums, but does the same job. If you have html code with unneeded ending tags it will strip them as well.

[edited by: PHP_Chimp at 11:32 am (utc) on Nov. 3, 2007]


2:04 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Modification -
$search = array('%>[^\S ]+%s', '%[^\S ]+<%s', '%(\s)+%s');
could be -
$search = array('%>[^\S ]+%', '%[^\S ]+<%', '%(\s)+%s');
As negative classes dont need the \s modifier. May speed it up a very little bit.

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.

[edited by: PHP_Chimp at 2:39 pm (utc) on Nov. 3, 2007]


Featured Threads

Hot Threads This Week

Hot Threads This Month