search
Homephp教程php手册php输出压缩HTML页面代码实例程序

php输出压缩HTML页面代码实例程序

Jun 13, 2016 am 09:51 AM
htmlphpcodecompressionremovecharacterExampleuseprogramchangeenteroutputpage

压缩页面输入就是把所有没有用的字符转去掉,然后所有代码放到一起,这样对于seo是有帮助,但是对于代码可读性很差,我们经常会看到很多网站这样做了,如果要手动来把html中字符空格删除很麻烦,于是就有了php输出压缩HTML页面实例了。

对于服务器输出的 HTML 代码,是否也可以进行压缩呢?

下面就是一个对 HTML 进行压缩的函数:

 

 代码如下 复制代码

function wpjam_minify_html($html) {

    $search = array(
        '/>[^S ]+/s',  // 删除标签后面空格
        '/[^S ]+',  // 删除标签前面的空格
        '/(s)+/s'       // 将多个空格合并成一个
    );

    $replace = array(
        '>',
        '         '\1'
    );

    $html = preg_replace($search, $replace, $html);

    return $html;
}

对于 WordPress 博客来说,将上面的函数和下面的代码复制到当前主题的 functions.php 文件中,就可以实现输出页面 HTML 代码的压缩:

 代码如下 复制代码


if(!is_admin()){
 add_action("wp_loaded", 'wp_loaded_minify_html');
 function wp_loaded_minify_html(){
  ob_start('wpjam_minify_html');
 }
}

当然上面的做法都是了网站seo优化了,我们有更好的办法就是结合上面的页面压缩输出再把服务器gzip压缩打开,这样页面会更小哦,关于 apacheapache服务器开启gzip压缩实例

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),