homepage Welcome to WebmasterWorld Guest from 54.145.172.149
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Compacting PHP files
Gian04

5+ Year Member



 
Msg#: 3492779 posted 4:54 pm on Oct 31, 2007 (gmt 0)

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

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



 
Msg#: 3492779 posted 4:57 pm on Oct 31, 2007 (gmt 0)

look at this thread
[webmasterworld.com...]

no linefeeds causes issues

dmorison

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3492779 posted 5:48 pm on Oct 31, 2007 (gmt 0)

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+ Year Member



 
Msg#: 3492779 posted 5:53 pm on Oct 31, 2007 (gmt 0)

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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3492779 posted 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.

dmorison

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3492779 posted 9:55 am on Nov 3, 2007 (gmt 0)

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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3492779 posted 11:31 am on Nov 3, 2007 (gmt 0)

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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3492779 posted 2:04 pm on Nov 3, 2007 (gmt 0)

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]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved