Home >Backend Development >PHP Tutorial >PHP output compressed HTML page code example program_PHP tutorial

PHP output compressed HTML page code example program_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:49:00880browse

Compressing page input is to remove all useless characters and then put all the codes together. This is helpful for SEO, but the readability of the code is very poor. We often see many websites doing this. It is very troublesome to manually delete character spaces in HTML, so there is an example of php output compressed HTML page.

Can the HTML code output by the server also be compressed?

The following is a function that compresses HTML:

The code is as follows Copy code
 代码如下 复制代码

function wpjam_minify_html($html) {

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

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

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

return $html;
}

function wpjam_minify_html($html) {

$search = array(
'/>[^S ]+/s', // Delete spaces after tags
          '/[^S ]+                                                                                                                                                                                       );
代码如下 复制代码


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

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

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

return $html;
}

For WordPress blogs, copy the above function and the following code into the functions.php file of the current theme to compress the HTML code of the output page: Of course, the above methods are all for website SEO optimization. We have a better way to combine the above page compression output and then turn on the server gzip compression, so that the page will be smaller. Regarding the example of turning on gzip compression on the apacheapache server http://www.bkjia.com/PHPjc/632740.html
www.bkjia.comtrue
http: //www.bkjia.com/PHPjc/632740.htmlTechArticleCompressing the page input is to remove all useless characters, and then put all the codes together, so that it is good for SEO Helpful, but for poor code readability we often see...
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