如何缩小 HTML 代码?

发布于 2024-11-04 03:45:38 字数 384 浏览 1 评论 0原文

我的想法是以某种方式缩小服务器端的 HTML 代码,以便客户端接收更少的字节。

“缩小”是什么意思?

不拉拉链。更像是,例如,jQuery 创建者使用 .min.js 版本。换句话说,我需要删除不必要的空格和换行符,但我无法删除太多 HTML 表示的更改(例如删除段落中实际单词之间的空格)。

有没有什么工具可以做到这一点?我知道有 HtmlPurifier。它能做到吗?还有其他选择吗?

PS 请不要提供正则表达式。我知道只有 Chuck Norris 可以用它们解析 HTML。 =]

My idea is to somehow minify HTML code in server-side, so client receive less bytes.

What do I mean with "minify"?

Not zipping. More like, for example, jQuery creators do with .min.js versions. In other words, I need to remove unnecessary white-spaces and new-lines, but I can't remove so much that presentation of HTML changes (for example remove white-space between actual words in paragraph).

Is there any tools that can do it? I know there is HtmlPurifier. Is it able to do it? Any other options?

P.S. Please don't offer regex'ies. I know that only Chuck Norris can parse HTML with them. =]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酸甜透明夹心 2024-11-11 03:45:38

有点晚了,但仍然......通过使用output_buffering,就这么简单:

function compress($string)
{
    // Remove html comments
    $string = preg_replace('/<!--.*-->/', '', $string);

    // Merge multiple spaces into one space
    $string = preg_replace('/\s+/', ' ', $string);   

    // Remove space between tags. Skip the following if
    // you want as it will also remove the space 
    // between <span>Hello</span> <span>World</span>.
    return preg_replace('/>\s+</', '><', $string);      
}

ob_start('compress');

// Here goes your html.    

ob_end_flush();

A bit late but still... By using output_buffering it is as simple as that:

function compress($string)
{
    // Remove html comments
    $string = preg_replace('/<!--.*-->/', '', $string);

    // Merge multiple spaces into one space
    $string = preg_replace('/\s+/', ' ', $string);   

    // Remove space between tags. Skip the following if
    // you want as it will also remove the space 
    // between <span>Hello</span> <span>World</span>.
    return preg_replace('/>\s+</', '><', $string);      
}

ob_start('compress');

// Here goes your html.    

ob_end_flush();
酒与心事 2024-11-11 03:45:38

您可以将 HTML 代码解析为 DOM 树(应在节点中保留内容空白),然后将其序列化回 HTML,而不需要任何美化空格。

You could parse the HTML code into a DOM tree (which should keep content whitespace in the nodes), then serialise it back into HTML, without any prettifying spaces.

冬天旳寂寞 2024-11-11 03:45:38

有没有什么工具可以做到这一点?

是的,这里有一个工具,您可以将其包含到构建过程中或用于 Web 缓存层:
https://code.google.com/archive/p/htmlcompressor/

或者,如果您正在寻找一个工具来缩小粘贴的 HTML,请尝试:
http://www.willpeavy.com/minifier/

Is there any tools that can do it?

Yes, here's a tool you could include into a build process or work into a web cache layer:
https://code.google.com/archive/p/htmlcompressor/

Or, if you're looking for a tool to minify HTML that you paste in, try:
http://www.willpeavy.com/minifier/

长亭外,古道边 2024-11-11 03:45:38

You can use the Pretty Diff tool: http://prettydiff.com/?m=minify&html It will also minify any CSS and JavaScript in the HTML code, and the minification occurs in a regressive manner so to not prevent future beautification of the HTML back to readable form.

破晓 2024-11-11 03:45:38

有没有什么工具可以做到这一点?

您可以使用CodVerter Online Web Development Editor来压缩混合html代码。
压缩机经过多次可靠性和准确性测试。
(全面披露:我是开发人员之一)。

“在此处输入图像描述"

Is there any tools that can do it?

You can use CodVerter Online Web Development Editor for compressing mixed html code.
the compressor was tested multiple times for reliability and accuracy.
(Full Disclosure: I am one of the developers).

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文