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

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

Jun 13, 2016 am 09:51 AM
html php code compression remove character Example use program change enter output page

压缩页面输入就是把所有没有用的字符转去掉,然后所有代码放到一起,这样对于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 of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles