如何像谷歌页面速度一样缩小我的 php 页面 html 输出?
例子:
<!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 ob_start,这个 html 输出怎么会变成这样?
<!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>
编辑:
我已经尝试过这段代码(基于 James Pegg 的回答)
function sanitize_output($buffer){
$search = array("\n", "\t");
return preg_replace($search, '', $buffer);}
ob_start("sanitize_output");
但它不起作用。该页面现在是空的。
最终工作代码(基于 W. Kristianto 的回答):
function sanitize_output($buffer){
$buffer = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $buffer);
return $buffer;}