Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:38:08+00:00 2026-06-13T01:38:08+00:00

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

  • 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;}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:38:09+00:00Added an answer on June 13, 2026 at 1:38 am
    $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>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to minify php page html output? I mean strip out all
Possible Duplicate: How to minify php page html output? Any reason not to strip
I followed the PHP Minify instructions listed on this page to use a sub-directory,
I need to minify my PHP's output in production and from this question it
Working with PHP DOM - html manipulation. It's really very effective way to output
Minify is a PHP5 minifier for JS and CSS files : http://code.google.com/p/minify/ When I
I have enabled google Minify in code igniter using this library on our website.
My client is using W3TC with the minify html/xml and when I enable that
From my gradle build I want to minify my html as part of the
I have an ad network script like the following http://cdn.domain.com/ad.php?variables=etc We have about 10,000

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.