PHP implementation of Web page HTML tag completion method _php Tutorial

Source: Internet
Author: User
If the HTML tag of your Web page is incomplete, some of the table tabs are incomplete and the page is cluttered, or if you include a partial HTML page outside your content, we can write a function method to complement the HTML tag and filter out the useless HTML tags.

PHP implements HTML tag completion method for Web pages

PHP makes the HTML tag auto-complete closed function method as follows:

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;

}

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

Array_search (): Array_search (value,array,strict), this function 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 parameter, strict, is specified as true, the key name of the corresponding element is returned only if the data type and value are consistent.


http://www.bkjia.com/PHPjc/735882.html www.bkjia.com true http://www.bkjia.com/PHPjc/735882.html techarticle if the HTML tag of your Web page is incomplete, some of the table tabs are incomplete and the page is cluttered, or if you include a partial HTML page outside your content, we can write a letter ...

  • Related Article

    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.