This post is automatically translated with LLM. The translation content has NOT been reviewed and may contain errors.
<body id="wordpress-org" class="home blog">
<div id="header">
<div class="wrapper">
<h1>[WordPress.org ](http://cn.wordpress.org/)</h1>
<h2 class="rosetta">
[China 简体中文 ](http://cn.wordpress.org/)
</h2>
<div style="clear:both"></div>
<ul>
<li>
<a href="/" title="首页" class="current"
>首页
</a>
</li>
<li>
<a
href="http://zh-cn.forums.wordpress.org/"
title="在此提出安装、使用上的问题,或与其它 WordPress 爱好者进行交流"
>论坛
</a>
</li>
<li>
<a
href="http://codex.wordpress.org/zh-cn:Main_Page"
title="WordPress 官方中文文档"
>文档
</a>
</li>
<li>
<a
href="http://codex.wordpress.org/Category:zh-cn:%E9%9C%80%E8%A6%81%E6%82%A8%E7%9A%84%E5%B8%AE%E5%8A%A9"
title="为中文 WordPress 做出贡献"
>贡献
</a>
</li>
<li>
<a
href="http://cn.wordpress.org/validators/"
title="WordPress China 当前各个项目的分工情况"
>团队
</a>
</li>
<li>
<a href="/contact/" title="联系译者"
>联系
</a>
</li>
</ul>
<div style="clear:both"></div>
</div>
</div>
<div class="outer" id="mid-wrapper">
<div class="wrapper">
<div class="section">
<h3>欢迎</h3>
<img
class="shot"
width="466"
height="303"
src="http://cn.wordpress.org/files/2011/07/dashboard-2.jpg"
alt="Localized version screenshot"
/>欢迎访问 WordPress
简体中文站点,这里提供可靠的官方
WordPress
中文版本以及相关支持。
WordPress
是一个注重美学、易用性和网络标准的个人信息发布平台。WordPress
虽为免费的开源软件,但其价值无法用金钱来衡量。
使用 WordPress
可以搭建功能强大的网络信息发布平台,但更多的是应用于个性化的博客。针对博客的应用,WordPress
能让您省却对后台技术的担心,集中精力做好网站的内容。
若您需要帮助,可以浏览我们的
[中文文档
](http://codex.wordpress.org/zh-cn:Main_Page)、在
[中文论坛
](http://zh-cn.forums.wordpress.org/)发帖,或者通过
[联系表单
](http://cn.wordpress.org/contact/)联系我们。祝您使用愉快!
</div>
</div>
</div>
</body>
Interested readers can count how many tab characters are in the code snippet above. This is just a small excerpt from cn.wordpress.org.
During HTML programming, to make code clear and understandable for website owners and technicians, developers often add hierarchical indentation using tabs or spaces. However, for end-users, these tabs and spaces are useless. Therefore, in the website's output, we should eliminate all these elements, including even line breaks—except for line breaks within textarea
and pre
content, which you'll understand need to be preserved.
A foreign expert developed an HTML Minify PHP file that can automatically remove these bandwidth-wasting elements. However, the original PHP execution efficiency was low because it included a feature to convert absolute paths to relative paths, which took longer than the time needed to remove the content. Therefore, I modified the code by removing this feature. Other modifications were also made, all sacrificing minor functionality to accelerate code execution.
Usage: Save this code as html-minify.php
, upload it to the same directory as your theme template's index.php
, and add the following line at the very top of your template's header.php
:
<?php include "html-minify.php"; ?>
Save the file, and you're done.
Below is my modified code: (For the original version, please visit http://github.com/stevenvachon/html-minify/)
<?php
/*
HTML Minify 0.5.7 <http://www.svachon.com/blog/html-minify/>
Reduce file size by shortening URLs and safely removing all standard comments and unnecessary white space from an HTML document.
*/
class HTML_Minify
{
// Settings
protected $compress_css;
protected $compress_js;
protected $info_comment;
protected $remove_comments;
// Variables
protected $html = '';
public function __construct($html, $compress_css=false, $compress_js=false, $remove_comments=true)
{
if ($html !== '')
{
$this->compress_css = $compress_css;
$this->compress_js = $compress_js;
$this->info_comment = $info_comment;
$this->remove_comments = $remove_comments;
$this->html = $this->minifyHTML($html);
}
}
public function __toString()
{
return $this->html;
}
protected function minifyHTML($html)
{
$pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si';
if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER) === false)
{
// Invalid markup
return $html;
}
$overriding = false;
$raw_tag = false;
// Variable reused for output
$html = '';
foreach ($matches as $token)
{
$tag = (isset($token['tag'])) ? strtolower($token['tag']) : null;
$content = $token[0];
$relate = false;
$strip = false;
if (is_null($tag))
{
$content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content);
$relate = true;
$strip = true;
}
else // All tags except script, style and comments
{
if ($tag === 'pre' || $tag === 'textarea')
{
$raw_tag = $tag;
}
else if ($tag === '/pre' || $tag === '/textarea')
{
$raw_tag = false;
}
else if (!$raw_tag && !$overriding)
{
if ($tag !== '')
{
// Remove any space before the end of a tag (including closing tags and self-closing tags)
$content = preg_replace('/\s+(\/?\>)/', '$1', $content);
// Do not shorten canonical URL
if ($tag !== 'link')
{
$relate = true;
}
else if (preg_match('/rel=(?:\'|\")\s*canonical\s*(?:\'|\")/i', $content) === 0)
{
$relate = true;
}
}
else // Content between opening and closing tags
{
// Avoid multiple spaces by checking previous character in output HTML
if (strrpos($html,' ') === strlen($html)-1)
{
// Remove white space at the content beginning
$content = preg_replace('/^[\s\r\n]+/', '', $content);
}
}
$strip = true;
}
}
if ($strip)
{
$content = $this->removeWhiteSpace($content, $html);
}
$html .= $content;
}
return $html;
}
protected function removeWhiteSpace($html, $full_html)
{
$html = str_replace("\t", '', $html);
$html = str_replace("\r", '', $html);
$html = str_replace("\n", '', $html);
return str_replace(' ', '', $html);
}
}
function html_minify_buffer($html)
{
return new HTML_Minify($html);
}
ob_start('html_minify_buffer');
?>```