0
Analyze HTML code
HTML generally includes the following parts:
Label to <> package
Document declaration and comments Format
Half-open tag <.../>
Close tags
Content, not wrapped with <>. multiple spaces in this part of content are ineffective.
PHP regular expression decomposition html code
Html code is bounded by tags. Therefore, you only need to break it down by tags. Use preg_split in PHP:
$segments = preg_split("/(<[^>]+?>)/si",$html, null,PREG_SPLIT_NO_EMPTY| PREG_SPLIT_DELIM_CAPTURE);
For example, the following code
$ Html = <
Wolf soul blog
Article RSS website map date archiving tag archiving Wolf soul blog
Focus on the WEB, understand life, cherish life, and stay away from code.
HTML; print_r (preg_split ("/(<[^>] +?>) /Si ", $ html,-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ));
The following output is generated:
Array ([0] =>[1] => [2] =>[3] => [4] =>[5] => [6] =>[7] => Wolf soul blog [8] =>[9] => [10] =>
[11] => [12] =>
[13] => [14] =>[15] => [16] =>[17] => [18] =>[19] => [20] => [21] => Article RSS [22] => [23] => [24] => [25] => website map [26] => [27] => [28] => [29] => date archiving [30] => [31] => [32] => [33] => tag archiving [34] => [35] => [36] => [37] => [38] => Wolf soul blog [39] => [40] => [41] => [42] =>
[43] => focus on the WEB and understand life; cherish life and stay away from code. [44] =>
[45] => [46] =>[47] =>
[48] => [49] =>[50] => [51] =>[52] => [53] =>)
Simplest but lossy PHP compression
The simplest compression method is to directly connect all the non-empty items and remove all the spaces without tags:
$ Compressed = array (); foreach ($ segments as $ seg) {$ seg = trim ($ seg); if ($ seg) {// The blank space in the non-tag is invalid. $ compressed [] = $ seg [0] ===' <'? $ Seg: preg_replace ('! \ S! ', '', $ Seg) ;}} return join ('', $ compress );
For example, the above HTML code generated through such compression is (to facilitate display, I manually disconnected them ):
Wolf soul blog
Article RSS website map date archiving tag archiving Wolf soul blog
Focus on the WEB, understand life, cherish life, and stay away from code.
Under normal circumstances, this is correct, but there are also "abnormal" situations: when blank content cannot be removed. For example, the script, code, pre, and style cannot be removed from the blank. in this case, we need to use the stack for compression:
Use stacks for secure html compression
The stack rules are as follows: <...> tag entry to stack, Tag output stack, And <.../> ignore, but there is a possibility that <.../> may not end with a backslash, such:
Yes. in this case, we need to handle this situation in a special way:
Article RSS website map date archiving tag archiving Wolf soul blog
Focus on the WEB, understand life, cherish life, and stay away from code.
var say = "Hello world!"; print say;
HTML; $ segments = preg_split ("/(<[^>] +?>) /Si ", $ html,-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $ compressed = array (); $ stack = array (); $ tag = ''; $ half_open = array ('meta', 'input', 'link', 'IMG ', 'Br'); $ cannot_compress = array ('pre', 'Code ', 'script', 'Style'); foreach ($ segments as $ seg) {if (trim ($ seg) ==='') {continue ;} // <... /> if (preg_match ("! <[A-z0-9] +) [^>] *? />! Si ", $ seg, $ match) {// $ tag = self: format_tag ($ match [1]); format_tag ($ match [1]); $ compressed [] = $ seg;} else if (preg_match ("!
] *?>! Si ", $ seg, $ match ))//
{$ Tag = format_tag ($ match [1]); if (count ($ stack)> 0 & $ stack [count ($ stack)-1] = $ tag) {array_pop ($ stack); $ compressed [] = $ seg;} // you 'd better add a piece of judgment here to fix the incorrect html //...} else if (preg_match ("! <[A-z0-9] +) [^>] *?>! Si ", $ seg, $ match) // <> {$ tag = format_tag ($ match [1]); // semi-closed tags do not need to be written into the stack, as shown in
, If (! In_array ($ tag, $ half_open) {array_push ($ stack, $ tag) ;}$ compressed [] = $ seg;} else if (preg_match ("~
] *> ~ ", $ Seg) {// The document declaration and comments, and the comments cannot be deleted, as shown in
$ Compressed [] = $ seg;} else {$ compressed [] = in_array ($ tag, $ cannot_compress )? $ Seg: preg_replace ('! \ S! ', '', $ Seg) ;}} function format_tag ($ tag) {return trim (strtolower ($ tag);} echo join ('', $ compressed );
The above code generates the following output (for ease of display, I manually disconnected ):
Article RSS website map date archiving tag archiving Wolf soul blog
Focus on the WEB, understand life, cherish life, and stay away from code.
var say = "Hello world!"; print say;
Most secure HTML compression
Of course, the basis for the above code to run correctly is: HTML is correct and legal, and does not appear as shown inBut there is no problem in common applications. if you need to securely compress HTML code, you can use the HTML parser library to fix and compress it.