Close open HTML tags in a string

后端 未结 9 1915
南方客
南方客 2020-11-28 11:21

Situation is a string that results in something like this:

This is some text and here is a bold text then the post stop here....

相关标签:
9条回答
  • 2020-11-28 12:00

    I've done this code witch doest the job quite correctly...

    It's old school but efficient and I've added a flag to remove the unfinished tags such as " blah blah http://stackoverfl"

    public function getOpennedTags(&$string, $removeInclompleteTagEndTagIfExists = true) {
    
        $tags = array();
        $tagOpened = false;
        $tagName = '';
        $tagNameLogged = false;
        $closingTag = false;
    
        foreach (str_split($string) as $c) {
            if ($tagOpened && $c == '>') {
                $tagOpened = false;
                if ($closingTag) {
                    array_pop($tags);
                    $closingTag = false;
                    $tagName = '';
                }
                if ($tagName) {
                    array_push($tags, $tagName);
                }
            }
            if ($tagOpened && $c == ' ') {
                $tagNameLogged = true;
            }
            if ($tagOpened && $c == '/') {
                if ($tagName) {
                    //orphan tag
                    $tagOpened = false;
                    $tagName = '';
                } else {
                    //closingTag
                    $closingTag = true;
                }
            }
            if ($tagOpened && !$tagNameLogged) {
                $tagName .= $c;
            }
            if (!$tagOpened && $c == '<') {
                $tagNameLogged = false;
                $tagName = '';
                $tagOpened = true;
                $closingTag = false;
            }
        }
    
        if ($removeInclompleteTagEndTagIfExists && $tagOpened) {
            // an tag has been cut for exemaple ' blabh blah <a href="sdfoefzofk' so closing the tag will not help...
            // let's remove this ugly piece of tag
            $pos = strrpos($string, '<');
            $string = substr($string, 0, $pos);
        }
    
        return $tags;
    }
    

    Usage example :

    $tagsToClose = $stringHelper->getOpennedTags($val);
    $tagsToClose = array_reverse($tagsToClose);
    
    foreach ($tagsToClose as $tag) {
        $val .= "</$tag>";
    }
    
    0 讨论(0)
  • 2020-11-28 12:07

    if tidy module is installed, use php tidy extension:

    tidy_repair_string($html)
    

    reference

    0 讨论(0)
  • 2020-11-28 12:07

    This is works for me to close any open HTML tags in a script.

    <?php
    function closetags($html) {
    preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
    $openedtags = $result[1];
    preg_match_all('#</([a-z]+)>#iU', $html, $result);
    $closedtags = $result[1];
    $len_opened = count($openedtags);
    if (count($closedtags) == $len_opened) {
        return $html;
    }
    $openedtags = array_reverse($openedtags);
    for ($i=0; $i < $len_opened; $i++) {
        if (!in_array($openedtags[$i], $closedtags)) {
            $html .= '</'.$openedtags[$i].'>';
        } else {
            unset($closedtags[array_search($openedtags[$i], $closedtags)]);
        }
    }
    return $html;
    }
    
    0 讨论(0)
提交回复
热议问题