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

前端 未结 10 2116
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:43

    Regardless of the 100 count issues you state at the beginning, you indicate in the challenge the following:

    • output the character count of strip_tags (the number of characters in the actual displayed text of the HTML)
    • retain HTML formatting close
    • any unfinished HTML tag

    Here is my proposal: Bascially, I parse through each character counting as I go. I make sure NOT to count any characters in any HTML tag. I also check at the end to make sure I am not in the middle of a word when I stop. Once I stop, I back track to the first available SPACE or > as a stopping point.

    $position = 0;
    $length = strlen($content)-1;
    
    // process the content putting each 100 character section into an array
    while($position < $length)
    {
        $next_position = get_position($content, $position, 100);
        $data[] = substr($content, $position, $next_position);
        $position = $next_position;
    }
    
    // show the array
    print_r($data);
    
    function get_position($content, $position, $chars = 100)
    {
        $count = 0;
        // count to 100 characters skipping over all of the HTML
        while($count <> $chars){
            $char = substr($content, $position, 1); 
            if($char == '<'){
                do{
                    $position++;
                    $char = substr($content, $position, 1);
                } while($char !== '>');
                $position++;
                $char = substr($content, $position, 1);
            }
            $count++;
            $position++;
        }
    echo $count."\n";
        // find out where there is a logical break before 100 characters
        $data = substr($content, 0, $position);
    
        $space = strrpos($data, " ");
        $tag = strrpos($data, ">");
    
        // return the position of the logical break
        if($space > $tag)
        {
            return $space;
        } else {
            return $tag;
        }  
    }
    

    This will also count the return codes etc. Considering they will take space, I have not removed them.

提交回复
热议问题