Introduction: when PHP generates static files, sometimes HTML tags are incomplete or messy due to some mixed Encoding Problems, resulting in page confusion. The author shares the following small code to help solve the problem.
If the html tags of Your webpage are not fully displayed, some table tags are incomplete, leading to page confusion, or some html pages other than your content are included, we can write a function to complete html tags and filter out useless html tags.
Php uses the following method to enable auto-completion of HTML tags:
Function closetags ($ html) {preg_match_all ('# <(?! Meta | img | br | hr | input \ B) \ B ([a-z] + )(? :.*)? (? # IU ', $ html, $ result); $ openedtags = $ result [1]; preg_match_all (' # iU ', $ html, $ result ); $ closedtags = $ result [1]; $ len_opened = count ($ openedtags); if (count ($ closedtags) ==$ len_opened) {return $ html ;} $ openedtags = array_reverse ($ openedtags); for ($ I = 0; $ I <$ len_opened; $ I ++) {if (! In_array ($ openedtags [$ I], $ closedtags) {$ html. = '';} else {unset ($ closedtags [array_search ($ openedtags [$ I], $ closedtags)]);}
} Return $ html ;}
Resolution:Array_reverse (): This function switches the elements in the original array sequentially, creates a new array, and returns the result. If the second parameter is set to true, the key name of the element remains unchanged; otherwise, the key name is lost. Array_search (): array_search (value, array, strict). This function looks for a key value in the array like in_array. If this value is found, the key name of the matching element is returned. If not found, false is returned. If the third parameter strict is set to true, the key name of the corresponding element is returned only when the data type and value are consistent.
Author blog address: http://blog.sina.com.cn/s/articlelist_2129618781_0_1.html
This article is an original article specially invited by PHPChina. It cannot be reproduced without permission. If you need to reprint it, please contact shixiang # phpchina.com (# Replace @)