Forum Moderators: coopster

Message Too Old, No Replies

How do I strip whitespace before serving a php page?

         

mr_nabo

11:15 am on Jan 9, 2009 (gmt 0)

10+ Year Member



Hi,

I've been told that to reduce download time I should be stripping whitespace and comments from my pages before I serve them to the browser (so that whitespace and comments are visible on the working copy, just not on the live copy).

Can anyone help me do this please?

Thanks

eelixduppy

4:04 pm on Jan 9, 2009 (gmt 0)



PHP has a function that does this: [php.net...] Perhaps this will help you.

mr_nabo

4:12 pm on Jan 9, 2009 (gmt 0)

10+ Year Member



Thanks eelixduppy,

But surely this is for stripping whitespace out of variables only right? How would I use it to strip out whitespaces from between html content?

ag_47

11:40 pm on Jan 9, 2009 (gmt 0)

10+ Year Member



hmm, very interesting. I was thinking about stripping newlines and comments from my files before uploading to server, as kind of a 'security' measure. I googled around a bit, actually didn't come across this function. I ended up writing a simple routine that takes care of all '//' comments and newlines. This way the 'live' version of the website is made up of one long sentence.

@mr_nabo, php_strip_whitespace seems to only affect PHP source code, ie. everything between <?php ... ?> The HTML code is left intact. I'm not sure if there are already functions for that, but you can make one yourself :)

Easy way: str_replace(array("\r", "\n"), '', php_strip_whitespace('file.php'));

Will give you file, with no comments and line breaks.