Using PHP substr() and strip_tags() while retaining formatting and without breaking HTML

前端 未结 10 2086
Happy的楠姐
Happy的楠姐 2020-11-27 16:44

I have various HTML strings to cut to 100 characters (of the stripped content, not the original) without stripping tags and without breaking HTML.

Original H

10条回答
  •  抹茶落季
    2020-11-27 17:22

    Not amazing, but works.

    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 .= "";
    
        return $result;
    }
    

    Usage example:

    $content = html_cut($content, 100);
    

提交回复
热议问题