我已经为缓存加载生成了PHP文件(通过file_get_contents()或include()随机程序...)让我们考虑这个例子:
未优化模式或未缓存模式:
我不想看到的内容:
<body>
和<head>
/> <script
或<table> <tr>
我想看到的内容(使用PHP的优化方式可能是PREG_REPLACE
):
<body><head>
/><script
或<table><tr>
上面的完整示例应该是RESULT,例如:
<!DOCTYPE><head><link href="/static/css/main.css" rel="stylesheet" type="text/css"><title>Title</title><meta http-equiv='Content-Type' content='Type=text/html; charset=UTF-8'> <meta name="description" content="Description site" /><meta name="keywords" content="keywords, another keywords" /><script type="text/javascript" src="/static/js/jquerymin.js"></script></head><body><div id="center_box"><div id="orderdata"><table></table></div><div id="orderform"><form method="post" action="/frontacp" method="post" action="/frontacp" method="post" action="/frontacp" method="post" action="/frontacp"><div class="logincol"><div class="leftcol"><label for="types_name">Tip proizvoda</label></div><div class="rightcol"><input type="text" name="types_name" /></form></div></div></body></html>
相反BAD(未经优化的用法)页面:
<!DOCTYPE><head>
<link href="/static/css/main.css" rel="stylesheet" type="text/css"><title>Title</title>
<meta http-equiv='Content-Type' content='Type=text/html; charset=UTF-8'> <meta name="description" content="Description site" /><meta name="keywords" content="keywords, another keywords" /><script type="text/javascript" src="/static/js/jquerymin.js"></script></head>
<body><div id="center_box">
<div id="orderdata">
<table>
</table>
</div>
<div id="orderform">
<form method="post" action="/frontacp" method="post" action="/frontacp" method="post" action="/frontacp" method="post" action="/frontacp"><div class="logincol"><div class="leftcol"><label for="types_name">Tip proizvoda</label></div><div class="rightcol"><input type="text" name="types_name" /></form> </div>
</div></body></html>
解决方案:PHP示例使用正确的方法修剪此问题,并且不要修剪空间内部HTML标记。
注意:我已经完成了阅读缓存和渲染这个问题不会涵盖在这个领域。
谢谢。
答案 0 :(得分:0)
这应该有效
$document = preg_replace( '/\n/', '', $document );
$document = preg_replace( '/>\s+</', '><', $document );
$document = preg_replace( '/\s{2,}/', ' ', $document );
再见
答案 1 :(得分:0)
这比人们想象的要复杂得多,它在这里提出了很多问题。我冒昧地找到一个似乎有一个合适的解决方案:
minifying-final-html-output-using-regular-expressions-with-codeigniter
(只是忽略CodeIgniter的引用,因为解决方案只是一个正则表达式)