Welcome to WebmasterWorld Guest from 54.234.95.190

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Compacting PHP files

     

Gian04

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

5+ 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?

jatar_k

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
[webmasterworld.com...]

no linefeeds causes issues

dmorison

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

Gian04

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

5+ 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.

londrum

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

<?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.

dmorison

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

WebmasterWorld Senior Member 10+ Year Member



Gian,

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...]

PHP_Chimp

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]

PHP_Chimp

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.

<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]

 

Featured Threads

Hot Threads This Week

Hot Threads This Month