|
- <?php
- function innerHTML($node) {
- return implode(array_map([$node->ownerDocument,"saveHTML"],
- iterator_to_array($node->childNodes)));
- }
- function closeUnclosedTags($html) {
- $dom = new DOMDocument();
- @$dom->loadHTML($html);
- // Get the body element
- $body = $dom->getElementsByTagName('body')->item(0);
- // Save only the HTML content of the body element
- return innerHTML($body);
- }
复制代码
測試:
- $html = '<div><p>This is some unclosed HTML';
- echo closeUnclosedTags($html);
复制代码
輸出:
<div><p>This is some unclosed HTML</p></div>
 |
|