Page 1 of 1

Removing whitespace on PHP output

Posted: Sun May 18, 2003 9:06 pm
by discobean
Howdi,

I've read somewhere a while ago that there was a way to remove whitespace from the output of PHP..

I'm just trying to cut down on bandwidth, and figured this would be a good place to start, does anybody have any suggestions?

Re: Removing whitespace on PHP output

Posted: Sun May 18, 2003 9:36 pm
by Kim
Hi :o

Code: Select all

<?php $string = "a  sf  adfaa as;df as fals fals fla sdlf asd f";
$pattern = "/ /i";
$replacement = "";
print preg_replace($pattern, $replacement, $string);?>

Posted: Sun May 18, 2003 9:52 pm
by discobean
Um...

I need something that replaces whitespace on output, not a regex function to replace whitespace in a string..

I've read that you can send all output to a function in php.ini using 'output_handler' but I can't find any documentation on how to implement this.

ie PHP outputs

Code: Select all

<html>
<head>
<body>

<table width="100%   ">
       <tr>
             <td></td>
       </tr>
</table>

</body>
</html>
I want it to replace to something like:

Code: Select all

<html>
<head>
<body>
<table width="100% ">
<tr>
<td></td>
</tr>
</table>
</body>
</html>

Posted: Sun May 18, 2003 10:14 pm
by Kim
Sorry, I don't know why do you want to do that ?

Code: Select all

print preg_replace("/^\s\</i","",$yourString);

Code: Select all

nl2br(htmlspecialchars($htmlString))
http://www.lexrus.com/Parser/

Posted: Sun May 18, 2003 10:23 pm
by discobean
I don't think you understand, I don't want to remove whitespaces out of a string of characters, I want to remove whitespaces from the output of PHP.

I need to do this in order to save bandwidth, unnecessary whitespace increases bandwidth, and I can allow for some decrease in performance, for increase in bandwidth.

Posted: Sun May 18, 2003 10:38 pm
by Kim
:oops: :oops:

I do not understand what you want! Sorry..........hihi!I am a Chinese...If you can understand what I say...Please:

I don't know you can save bandwidth with this.
:roll:

Posted: Mon May 19, 2003 3:41 am
by volka

Code: Select all

<?php 
function compress_whitespace($output) 
{ 
	$pattern1 = "!>\s+<!m"; $replace1 = '><';
	$pattern2 = "!\s{2,}!m"; $replace2 = ' ';
	
	return preg_replace($pattern2, $replace2, 
			preg_replace($pattern1, $replace1, $output)
		);
} 

ob_start('compress_whitespace'); 
?>
<html>
	<head>
		<title>just a test</title>
	</head>
	<body>
		<p>
			decrease bandwidth
			on cost of performance
		</p>
	</body>
</html>
But this takes two regular expressions, hope it's worth it.
gzip/deflate compression already is enabled?

Posted: Mon May 19, 2003 7:57 pm
by discobean
works good..

Thanks mate