A complete method of HTML tag in PHP implementation

Source: Internet
Author: User

If the HTML tags on your Web page are not fully displayed, some form tags are incomplete and the page is cluttered, or include local HTML pages that are outside of your content, we can write a functional approach to complement all HTML tags and filter out unwanted HTML tags.

A complete method of HTML tag in PHP implementation

PHP makes HTML tags automatically complement the closed function method as follows:

function Closetags ($html) {

Preg_match_all (' #< (?!) metaimgbrhrinput\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;

}


Array_reverse (): This function flips the order of elements in the original array, creates a new array, and returns. If the second argument is specified as true, the key name of the element remains unchanged or the key is lost.

Array_search (): Array_search (value,array,strict), which looks for a key value in the array like In_array (). If the value is found, the key name of the matching element is returned. Returns False if it is not found. If the third argument strict is specified as true, the key name of the corresponding element is returned only if both the data type and the value are consistent.




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.