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;}

```