At 04:37 PM 9/4/02 , Matt Zur wrote:
>How do I remove the whitespace from a document? I consulted the manual and 
>it said to use the trim function.  But in a large PHP document, with lots 
>of fuctions etc in PHP... is there a simple way to compress the whitespace 
>of the entire document?  Rather than going through each var and using the 
>trim?  Like add a header at the top:  <?PHP compresswhitespace() ?>
>
>
>If you have a 300k php document, won't the source code reveal (after the 
>browser displays the page) a bunch of whitespace.  Doesn't this add to dl 
>time and if so, how do I get rid of it.

Not 100% sure if I understand the drift of your question, but here's a few 
possibilities.

(1) Are you concerned about whitespace you put in a PHP program to enhance 
readability slowing down page loads?

Any whitespace WITHIN PHP tags does not get sent to the browser at all. So 
you don't need to worry about it.

(2) Are you concerned that the OUTPUT of your PHP program and/or HTML 
embedded therein might have excessive whitespace? If so, you could create 
an output handler that cleans up the output before sending, I think; see

         http://www.php.net/manual/en/ref.outcontrol.php

Or, use compression. See:

         http://www.php.net/manual/en/function.ob-gzhandler.php

(3) If you simply wanted to clean up excessive whitespace in a file, you 
could use a snippet like:

         ...read file into $file variable

         # Replace multiple spaces/tabs with a space
         $file = preg_replace('/[ \t]{2,}/','/ /', $file);
         # Replace multiple lines with single line
         $file = preg_replace('/(\n|\r|\r\n){2,}/', '\r', $file);

         ...write $file back out

That last line for replacing multiple blank lines is probably wrong; at the 
very least you'd have to do some platform checking (Win vs. Mac vs. Unix) 
to get the proper line ending. You might also be able to use the pattern 
'/(^$){2,}/m'

In PHP4 I think you can use arrays:

         ...read in
         $file = preg_replace(
                         array('/[ \t]{2,}/', '/(\n|\r|\r\n){2,}/'),
                         array('/ /', '\r'),
                         $file
                 );
         ...write out

See

         http://www.php.net/manual/en/function.preg-replace.php
         http://www.php.net/manual/en/pcre.pattern.syntax.php
         http://www.php.net/manual/en/pcre.pattern.modifiers.php

-steve


+------------------------------------------------------------------------+
| Steve Edberg                                      [EMAIL PROTECTED] |
| Database/Programming/SysAdmin                            (530)754-9127 |
| University of California, Davis             http://pgfsun.ucdavis.edu/ |
+---------------------- Gort, Klaatu barada nikto! ----------------------+

Reply via email to