0

How to minify my php page html output like google page speed does?

Example:

<!DOCTYPE html>
<html>

<head>
<title>Untitled Document</title>

<meta name="description" content="Phasellus rhoncus euismod libero a lacinia. Lorem ipsum dolor sit amet, consectetur adipiscing elit." />

</head>

<body>
<div id="content">
<div id="post-1">
<h1>Phasellus rhoncus euismod libero a lacinia.</h1>
<p>...</p>
</div>

<div id="post-2">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<p>...</p>
</div>

</div>


</body>
</html>

How can this html output can become something like this using php ob_start?

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title> 
<meta name="description" content="Phasellus rhoncus euismod libero a lacinia. Lorem ipsum dolor sit amet, consectetur adipiscing elit." />
</head>
<body>
<div id="content">
<div id="post-1">
<h1>Phasellus rhoncus euismod libero a lacinia.</h1>
<p>...</p>
</div>
<div id="post-2">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<p>...</p>
</div>
</div>
</body>
</html>

EDIT:

I've tried this code (based on James Pegg's answer)

function sanitize_output($buffer){
$search = array("\n", "\t");
return preg_replace($search, '', $buffer);}

ob_start("sanitize_output");

But it doesn't work. The page is now empty.

Final working code (based on W. Kristianto's answer):

function sanitize_output($buffer){
$buffer = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $buffer);
return $buffer;}
2
  • I find your question interesting, however, in real life you should first optimise elsewhere. Your example code goes from 458 to 451 bytes (or 257 to 251 bytes after gzipping it). You're adding another processing layer just to shave off a couple of bytes. Look at smaller images, fewer requests and caching instead. Commented Oct 15, 2012 at 19:52
  • I already use gzip for my pages but i wanted to know how can this can be acomplished. Some time ago i used wordpress with a caching plugin (i don't remember the name) that was able to do this (minify the html code). Commented Oct 15, 2012 at 19:59

4 Answers 4

1
$data = '
<!DOCTYPE html>
<html>

<head>
<title>Untitled Document</title>

<meta name="description" content="Phasellus rhoncus euismod libero a lacinia. Lorem ipsum dolor sit amet, consectetur adipiscing elit." />

</head>

<body>
<div id="content">
<div id="post-1">
<h1>Phasellus rhoncus euismod libero a lacinia.</h1>
<p>...</p>
</div>

<div id="post-2">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<p>...</p>
</div>

</div>


</body>
</html>';

echo "<pre>";
echo htmlentities(preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $data));
echo "</pre>";
Sign up to request clarification or add additional context in comments.

1 Comment

i used that code without htmlentities. only the regex and now it works.
0

Just remove every line which contains only \r\n, or \r, or \n.

This can be useful: How do I remove blank lines from text in PHP?

Comments

0

You could take out all new lines and tabs like this:

<?php
// Start Output Bugger
ob_start();

// Start HTML Output
?>
<!DOCTYPE html>
<html>

<head>
<title>Untitled Document</title>

<meta name="description" content="Phasellus rhoncus euismod libero a lacinia. Lorem ipsum dolor sit amet, consectetur adipiscing elit." />

</head>

<body>
<div id="content">
<div id="post-1">
<h1>Phasellus rhoncus euismod libero a lacinia.</h1>
<p>...</p>
</div>

<div id="post-2">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<p>...</p>
</div>

</div>


</body>
</html>

<?php

// Save Output bugger as a variable
$html = ob_get_clean();

//Array of characters to replace
$strings = Array("\n", "\t");

//Strip out new lines and tabs
$html = str_replace($strings, '', $html);

//Echo minified content
echo $html;
?>

Hope that helps!

1 Comment

You need to use str_replace() in your example, not preg_replace(). Then it'll work as expected. You can't give preg_replace an array of regular expressions, but you can give str_replace() an array of strings to search for. Also be careful when using regular expressions, they can be slower in cases like these.
0

Try to use some js library, some times its useful

1 Comment

Thank you, but i don't want to minify/optimize the js code. I want to optimize the html code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.