Removing whitespace on PHP output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Removing whitespace on PHP output

Post 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?
User avatar
Kim
Forum Newbie
Posts: 6
Joined: Sun May 18, 2003 9:36 pm

Re: Removing whitespace on PHP output

Post 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);?>
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post 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>
User avatar
Kim
Forum Newbie
Posts: 6
Joined: Sun May 18, 2003 9:36 pm

Post 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/
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post 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.
User avatar
Kim
Forum Newbie
Posts: 6
Joined: Sun May 18, 2003 9:36 pm

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post by discobean »

works good..

Thanks mate
Post Reply