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.