Home > Backend Development > PHP Tutorial > How to implement web page tag completion in php_PHP tutorial

How to implement web page tag completion in php_PHP tutorial

WBOY
Release: 2016-07-13 10:39:03
Original
755 people have browsed it

Introduction: When PHP generates static files, sometimes due to some mixing problems, HTML tags are incomplete or confusing, resulting in page confusion. The author shares the following small code that can solve the problem very conveniently.


If the html tags of your web page content are not fully displayed, some table tags are incomplete and cause page confusion, or partial html pages other than your content are included, we can write a function method to complete the html tags and filter Remove useless html tags.
The method for PHP to automatically complete the closing function of HTML tags is as follows:
Copy after login
function closetags($html) { preg_match_all('#<(?!meta|img|br|hr|inputb)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; }

Analysis: array_reverse(): This function reverses the order of elements in the original array, creates a new array and returns it. If the second parameter is specified as true, the element's key name remains unchanged, otherwise the key name is lost. array_search(): array_search(value, array, strict), this function searches for a key value in the array just like in_array(). If the value is found, the key of the matching element is returned. If not found, returns false. If the third parameter strict is specified as true, the key name of the corresponding element will only be returned if the data type and value are consistent.





Author's blog address: http://blog.sina.com.cn/s/articlelist_2129618781_0_1.html
This article is an original article by a specially invited author of PHPChina. It may not be reproduced without permission. If you need to reprint, please contact shixiang#phpchina.com (# replaced by @)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735052.htmlTechArticleIntroduction: When PHP generates static files, sometimes the HTML tags will be incomplete or incomplete due to some mixing problems. Confusion leads to cluttered pages. The author shared the following small code which can be very convenient...
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