Page 1 of 1

Is there a way to remove all white space from document?

Posted: Mon Jan 05, 2009 9:50 pm
by lovelf
how could I remove space in blank after ">" character?

sounds simple, I have tried:

Code: Select all

<?php function callback($buffer) {$buffer = str_replace('> ', '>', $buffer); return $buffer;} ob_start("callback"); ?>
But of course it just does not make it, since if there is more than one space after ">" it does not get replaced.

Same for:

Code: Select all

<?php function callback($buffer) {$buffer = str_replace('>
', '>', $buffer); return $buffer;} ob_start("callback"); ?>
In this other one if you have more than one line break inside the document those do not get replaced.

Re: Is there a way to remove all white space from document?

Posted: Mon Jan 05, 2009 9:55 pm
by Eran
Probably inefficient, but will do what you ask:

Code: Select all

<?php
function callback($buffer) {
    while(strpos($buffer,'> ') !== false) {
        $buffer = str_replace('> ', '>', $buffer);
    }
     return $buffer;
} 
ob_start("callback");

Re: Is there a way to remove all white space from document?

Posted: Mon Jan 05, 2009 10:07 pm
by lovelf
Thanks, that did it :wink:

Re: Is there a way to remove all white space from document?

Posted: Mon Jan 05, 2009 10:54 pm
by requinix
But if the "> " is positioned one character before the end of the current buffer, the callback will only see a > at the end and the next call will have a space at the beginning.

Code: Select all

$buffer = preg_replace('/>\s+/s', '>', $buffer);
I'd use that on the entire text, not just a part of it.