Php web page tag Completion Method

Source: Internet
Author: User

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 @)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.