Removing whitespace on PHP output
Moderator: General Moderators
- discobean
- Forum Commoner
- Posts: 49
- Joined: Sun May 18, 2003 9:06 pm
- Location: Sydney, Australia
- Contact:
Removing whitespace on PHP output
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?
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
Hi
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);?>- discobean
- Forum Commoner
- Posts: 49
- Joined: Sun May 18, 2003 9:06 pm
- Location: Sydney, Australia
- Contact:
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
I want it to replace to something like:
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>Code: Select all
<html>
<head>
<body>
<table width="100% ">
<tr>
<td></td>
</tr>
</table>
</body>
</html>Sorry, I don't know why do you want to do that ?
http://www.lexrus.com/Parser/
Code: Select all
print preg_replace("/^\s\</i","",$yourString);Code: Select all
nl2br(htmlspecialchars($htmlString))- discobean
- Forum Commoner
- Posts: 49
- Joined: Sun May 18, 2003 9:06 pm
- Location: Sydney, Australia
- Contact:
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.
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.
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>gzip/deflate compression already is enabled?