如何缩小 HTML 代码?
我的想法是以某种方式缩小服务器端的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有点晚了,但仍然......通过使用output_buffering,就这么简单:
A bit late but still... By using output_buffering it is as simple as that:
您可以将 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.
是的,这里有一个工具,您可以将其包含到构建过程中或用于 Web 缓存层:
https://code.google.com/archive/p/htmlcompressor/
或者,如果您正在寻找一个工具来缩小粘贴的 HTML,请尝试:
http://www.willpeavy.com/minifier/
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/
您可以使用 Pretty Diff 工具:http://prettydiff.com/?m=minify&html 它还会缩小 HTML 代码中的任何 CSS 和 JavaScript,并且缩小以回归方式进行,因此不会阻止将来将 HTML 美化回可读形式。
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.
您可以使用CodVerter Online Web Development Editor来压缩混合html代码。
压缩机经过多次可靠性和准确性测试。
(全面披露:我是开发人员之一)。
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).