PHP implementation of the closing HTML tag _php tips

Source: Internet
Author: User

This article is an example of the PHP implementation completion of the closed HTML tag. Share to everyone for your reference, specific as follows:

Many times, when we make a fuss about intercepting a summary, if the content of the HTML appears, there will be an HTML tag that intercepts the article without ending. In this case, there will be problems with the page style disorder. This time we need to add the missing end tag in batches. In the Www.php.net official website to see a relatively good deal with a function, the display is as follows:

function Closetags ($html) {//Strip fraction of open or close tag from-end (e.g. if we take a-X characters, we-MIG
  HT cut off a tagged at the end!) $html = preg_replace ('/<[^>]*$/', ', ', $html); Ending with fraction of the open tag//put open tags into an array preg_match_all (' #< ([a-z]+) (?:. *)? <! [/|/ ])
  > #iU ', $html, $result);
  $opentags = $result [1];
  Put all closed tags into an array preg_match_all (' #</([a-z]+) > #iU ', $html, $result);
  $closetags = $result [1];
  $len _opened = count ($opentags);
  If all tags are 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 not skip tags, can have content inside! For ($i =0 $i < $len _opened $i + +) {$ot = Strtolower ($opentags[$i]); if (!in_array ($opentags [$i], $closetags) &&!in_array ($ot, $SC)) {$html. = ' </'. $opentags [$i]. '
   > ';
   else {unset ($closetags [Array_search ($opentags [$i], $closetags)]);
} return $html;

 }

Results of test usage:

<?php
$content = ' <div><p><span> more busy you, do you want to give yourself a vacation? Focus on your work, do you remember when the last time you exercised? Excellent with outdoor travel, give you a different travel experience: give heart freedom, it is everywhere scenery! ';
echo Closetags ($content);
/* Return results:
<div><p><span> More
busy you, do you want to give yourself a vacation? Focus on your work, do you remember when the last time you exercised? Excellent with outdoor travel, give you a different travel experience: give heart freedom, it is everywhere scenery! </span></p></div>
*
?>

For more information on PHP-related content readers can view the site topics: "PHP operation Office Document tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course", "PHP string (string) Summary of usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques

I hope this article will help you with the PHP program design.

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.