【问题标题】:How to cut an HTML text in PHP without breaking the tags hierarchy如何在 PHP 中剪切 HTML 文本而不破坏标签层次结构
【发布时间】:2015-05-11 05:28:06
【问题描述】:

我正在尝试修剪一些 HTML 文本并找到一个线程,但由于我是新人 (Using PHP substr() and strip_tags() while retaining formatting and without breaking HTML) 还不能评论它

首先我创建了函数 preview(输入: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 标签,这个函数的结果可能会被破坏

标签: php html text trim strip-tags


【解决方案1】:

尝试使用HTML parserTidy HTML. 用于检查嵌套标签

【讨论】:

  • 我需要安装tidy功能吗?页面说(此扩展与 PHP 5 及更高版本捆绑在一起,并使用 --with-tidy 配置选项安装。)我该如何称呼它
  • 我正在阅读这个函数的文档,但是我需要在没有这个设置的情况下工作,因为这是一个自安装系统,我不能把那种工作留给用户,因为太混乱了
猜你喜欢
  • 2011-01-14
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 2021-06-23
  • 1970-01-01
相关资源
最近更新 更多