Home > Backend Development > PHP Tutorial > PHP compresses html web page code: clear spaces, tabs, comment marks_PHP tutorial

PHP compresses html web page code: clear spaces, tabs, comment marks_PHP tutorial

WBOY
Release: 2016-07-13 17:49:59
Original
1093 people have browsed it

If you want to improve the loading speed of web pages, how to optimize it is a question. Yahoo once made an optimization with 36 items. In fact, there are many ways to optimize web pages. Here are some ways to reduce page size to improve front-end loading speed:
PHP compresses html web page code (remove spaces, newlines, tabs, comment marks).
A good way is to compress HTML. Compressing HTML actually means: clearing newlines, clearing tabs, and removing comment marks. The role it plays cannot be underestimated.
Now provides PHP compressed HTML function. Please give it a try, it feels good.

No more nonsense, just go to the code:

1 2 /**
3 * Compress html: clear newlines, clear tabs, remove comment marks
4 * @param $string
5 * @return Compressed $string
6**/
7 function compress_html($string) {
8 $string = str_replace("rn", '', $string);
//Clear newlines
9 $string = str_replace("n", '', $string);
//Clear newlines
10 $string = str_replace("t", '', $string);
//Clear tab characters www.2cto.com
11 $pattern = array (
12 "/> *([^ ]*) * //Remove comment mark
13 "/[s]+/",
14               "//",
15              "/" /", 
16              "/ "/", 
17 "'/*[^*]**/'"
18               ); 
19 $replace = array (
20 ">\1<",
21 " ",
22 "",
23 """,
24 """,
25 ""
26 );
27 return preg_replace($pattern, $replace, $string);
28 }
29 ?>
Eliminate line numbers

Excerpted from Xiaohanzi

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478294.htmlTechArticleIf you want to improve the webpage loading speed, how to optimize it is a question. Yahoo once made an optimization with 36 items. In fact, there are many ways to optimize web pages. Let’s talk about reducing...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template