gpt4 book ai didi

html - 如何缩小 HTML 代码?

转载 作者:太空狗 更新时间:2023-10-29 13:39:17 25 4
gpt4 key购买 nike

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

“缩小”是什么意思?

不压缩。更像是,例如,jQuery 创建者使用 .min.js 版本。换句话说,我需要删除不必要的空格和换行符,但我不能删除太多以至于 HTML 的表示发生变化(例如删除段落中实际单词之间的空格)。

有什么工具可以做到吗?我知道有 HtmlPurifier .它能做到吗?还有其他选择吗?

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

最佳答案

有点晚了,但仍然......通过使用 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();

关于html - 如何缩小 HTML 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5815755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com