如何在不破坏标签层次结构的情况下在 PHP 中剪切 HTML 文本


How to cut an HTML text in PHP without breaking the tags hierarchy

我正在尝试修剪一些HTML文本并找到一个线程,但还无法在其中发表评论,因为我是新手(使用PHP substr()和strip_tags(),同时保留格式并且不破坏HTML)

首先,我创建了函数预览(输入:html文本或纯文本,字符数,布尔值(如果需要纯文本输出),但是当我尝试扩展功能以使用HTML标签时,问题开始了

我使用其他帖子中的函数html_cut()来关闭标签,但我需要一些嵌套标签,我认为该函数关闭了它找到的每个标签,因此它打破了层次结构。(这实际上是问题还是我错了?

function preview($text, $char, $sinhtml){
    if(strlen($text) > $char){
        $post = substr($text, $char, 1);
        if ($post != " "){
            $i = true;
            while($post != " "){
                if($char > 0 && $i){
                    $char--;
                    $post = substr($text, $char, 1);
                }elseif($char == 0){
                    $i = false;
                    $char++;
                }else{
                    $char++;
                    $post = substr($text, $char, 1);
                }
            }
        }
        $post = substr($text, 0, $char);
        $post .= " …";
        if($sinhtml){
            return strip_tags($post);
        }else{
-->         return $post;
        }
    }else{
        return $text;
    }
}

输入文本是这样的

<p> Some text… </p>
<ul>
   <li>Technical Description</li>
   <li>or Details (weight, size, etc.)</li>
   <li>…</li>
</ul>
<p>may be some more text</p>

函数html_cut()有一条我以前从未见过的线,也不知道它的作用......$symbol = $text{$i}

function html_cut($text, $max_length)
{
    $tags   = array();
    $result = "";
    $is_open   = false;
    $grab_open = false;
    $is_close  = false;
    $in_double_quotes = false;
    $in_single_quotes = false;
    $tag = "";
    $i = 0;
    $stripped = 0;
    $stripped_text = strip_tags($text);
    while ($i < strlen($text) && $stripped < strlen($stripped_text) && $stripped < $max_length)
    {
        $symbol  = $text{$i};
        $result .= $symbol;
        switch ($symbol)
        {
           case '<':
                $is_open   = true;
                $grab_open = true;
                break;
           case '"':
               if ($in_double_quotes)
                   $in_double_quotes = false;
               else
                   $in_double_quotes = true;
            break;
            case "'":
              if ($in_single_quotes)
                  $in_single_quotes = false;
              else
                  $in_single_quotes = true;
            break;
            case '/':
                if ($is_open && !$in_double_quotes && !$in_single_quotes)
                {
                    $is_close  = true;
                    $is_open   = false;
                    $grab_open = false;
                }
                break;
            case ' ':
                if ($is_open)
                    $grab_open = false;
                else
                    $stripped++;
                break;
            case '>':
                if ($is_open)
                {
                    $is_open   = false;
                    $grab_open = false;
                    array_push($tags, $tag);
                    $tag = "";
                }
                else if ($is_close)
                {
                    $is_close = false;
                    array_pop($tags);
                    $tag = "";
                }
                break;
            default:
                if ($grab_open || $is_close)
                    $tag .= $symbol;
                if (!$is_open && !$is_close)
                    $stripped++;
        }
        $i++;
    }
    while ($tags)
        $result .= "</".array_pop($tags).">";
    return $result;
}

尝试使用 HTML 解析器或整洁的 HTML。用于检查嵌套标签