How to close unclosed HTML Tags?

后端 未结 8 576
名媛妹妹
名媛妹妹 2020-11-30 01:56

Whenever we are fetching some user inputed content with some editing from the database or similar sources, we might retrieve the portion which only contains the opening tag

8条回答
  •  执笔经年
    2020-11-30 02:14

    I used to the native DOMDocument method, but with a few improvements for safety.

    Note, other answers that use DOMDocument do not consider html strands such as

    This is a HTML strand
    

    The above will actually result in

    This is a HTML strand

    My Solution is below

    function closeDanglingTags($html) {
        if (strpos($html, '<') || strpos($html, '>')) {
            // There are definitiley HTML tags
            $wrapped = false;
            if (strpos(trim($html), '<') !== 0) {
                // The HTML starts with a text node. Wrap it in an element with an id to prevent the software wrapping it with a 

    // that we know nothing about and cannot safely retrieve $html = cHE::getDivHtml($html, null, 'closedanglingtagswrapper'); $wrapped = true; } $doc = new DOMDocument(); $doc->encoding = 'utf-8'; @$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); if ($doc->firstChild) { // Test whether the firstchild is definitely a DOMDocumentType if ($doc->firstChild instanceof DOMDocumentType) { // Remove the added doctype $doc->removeChild($doc->firstChild); } } if ($wrapped) { // The contents originally started with a text node and was wrapped in a div#plasmappclibtextwrap. Take the contents // out of that div $node = $doc->getElementById('closedanglingtagswrapper'); $children = $node->childNodes; // The contents of the div. Equivalent to $('selector').children() $doc = new DOMDocument(); // Create a new document to add the contents to, equiv. to "var doc = $('');" foreach ($children as $childnode) { $doc->appendChild($doc->importNode($childnode, true)); // E.g. doc.append() } } // Remove the added html,body tags return trim(str_replace(array('', ''), '', html_entity_decode($doc->saveHTML()))); } else { return $html; } }

提交回复
热议问题