PHP implementation of closing HTML tags, PHP complement HTML tags _php tutorial

Source: Internet
Author: User
Tags getting started with php

PHP implementation of closed HTML tags, PHP complement HTML tags


The example in this article describes the HTML tags that PHP implements to close. Share to everyone for your reference, as follows:

Most of the time, when we make a fuss about intercepting abstracts, if there is HTML content, there will be an HTML tag that does not end with the truncated article. In this case, there will be a problem with page style confusion. What we need at this point is to add the missing end tag to the batch. In the Www.php.net official website to see a relatively good processing of a function, shown as follows:

function Closetags ($html) {//Strip fraction of open or close tag from end (e.g. if we take first x characters, we might  Cut off a tag at the end!) $html = preg_replace ('/<[^>]*$/', ' ', $html); Ending with fraction of open tag//Put open tags to an array preg_match_all (' #< [a-z]+] (?:. *)?
 #iU ', $html, $result);  $opentags = $result [1]; Put all closed tags to an array preg_match_all (' #
 #iU ', $html, $result);  $closetags = $result [1];  $len _opened = count ($opentags);  If all tags is closed, we can return if (count ($closetags) = = $len _opened) {return $html;  }//Close tags in reverse order that they were opened $opentags = Array_reverse ($opentags);  Self closing tags $SC = array (' br ', ' input ', ' img ', ' hr ', ' meta ', ' link ');  , ' frame ', ' iframe ', ' param ', ' area ', ' base ', ' Basefont ', ' col '//should don't skip tags that can has content inside!   for ($i =0; $i < $len _opened; $i + +) {$ot = Strtolower ($opentags [$i]); if (!in_array ($opentags [$i], $closetags) &&!in_array ($ot, $SC)) {$html. = '
 ';   } else {unset ($closetags [Array_search ($opentags [$i], $closetags)]); }} return $html;}

Results of test use:

<?php$content = '

Do you want to give yourself a vacation if you are getting busy? Focus on your work, do you still remember when the last exercise was? Excellent partner outdoor Travel, give you a different travel experience: to the heart of freedom, then everywhere is the scenery! '; Echo closetags ($content);/* The result returned is:

Do you want to give yourself a vacation if you are getting busy? Focus on your work, do you still remember when the last exercise was? Excellent partner outdoor Travel, give you a different travel experience: to the heart of freedom, then everywhere is the scenery!

*/?>

More readers interested in PHP related content can view this site topic: "PHP Operations Office Document Tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP Object-oriented Programming tutorial", "PHP string (string) Usage summary, "Getting Started with Php+mysql database operation" and "PHP common database Operation Skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP implementation of HTML tag closure detection and repair method
    • PHP implementation filters various HTML tags
    • How PHP intercepts HTML strings and automatically complements HTML tags
    • PHP Implementation method to filter the HTML tags in the form submission
    • PHP filter HTML tags, attributes, etc. regular expression summary
    • PHP function sharing for intercepting strings containing HTML tags
    • PHP determines if the string contains HTML tags
    • PHP gets page title and content functions (without HTML tags)
    • HTML tag filtering tips in PHP
    • PHP Removes HTML tag instances
    • PHP three ways to remove HTML tags
    • PHP makes HTML tags auto-complete closed function code
    • PHP Implements HTML tag auto-complete code
    • PHP regular filter code for HTML tags, spaces, line breaks (with instructions)
    • Code to empty HTML tags in a string under PHP

http://www.bkjia.com/PHPjc/1113720.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113720.html techarticle PHP implementation of closed HTML tags, PHP complement HTML tags This article describes the PHP implementation of closing HTML tags. Share to everyone for your reference, specific as follows: Many times, in ...

  • 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.