Question:
How to remove tabs, line breaks, unneeded spaces, etc?
only4u
2006-12-21 00:30:39 UTC
I am trying to save a little bandwidth by removing line breaks, tabs, unneeded spaces, etc. that you would see in a browser's view source. Is there a way of doing this when I'm using a php template class?

Example:
Google.com. If you view their source, the code is cropped.

Info:
I have read some information regarding php's ob_start but I am not sure if that would apply to me as the examples I've seen echo's the buffer, whereas the template class is derived from phpbb and it has it's own $template method to make the output.

Example of method I use:
$tpl->set_filenames(array(
'body' => 'index.tpl')
);
$tpl->loadfile('body');
$tpl->pparse('body');

My HTML code is in index.tpl, and the PHP code is in index.php. The $tpl call's the template class and builds the page on pparse.
Three answers:
jan
2006-12-21 00:34:44 UTC
Far better to talk to the people here

http://www.php.net/

///
J.L.
2006-12-21 09:32:43 UTC
You're best bet is to learn a bit about what's called Regular Expressions. A regular expression is a string that describes or matches a set of strings, according to certain syntax rules. See: http://en.wikipedia.org/wiki/Regular_expression for more details.



I use a freeware program called "Regex Coach" which allows me to view the regular expression I'm attempting to use against a sample text I'm searching against.



Particularly, you want to try to use PHP's function call preg_replace. See: http://us3.php.net/preg_replace



So you'd want to basically create an array with a bunch of regular expressions to search for like:



regexar[0] = '\t' # Get rid of any tabs

regexar[1] = '\s\s' # Get rid of any double spaces

regexar[2] = '\n\n' # Double line breaks (I didn't know if you want the whole thing on a single line or not...)



And so on.



Then for your replacement array, go ahead and populate the array with:



replacement[0] = ''



Etc. Use a loop to set your array up... likely to be a lot faster.



I hope this helps!
Cva
2006-12-21 08:36:28 UTC
Edit the .tpl file and remove unneeded breaks or spaces, mind u it might change the look and feel, better keep a backup of the file.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...