How to get first x chars from a string, without cutting off the last word?

后端 未结 13 1276
再見小時候
再見小時候 2020-11-28 10:24

I have the following string in a variable.

Stack Overflow is as frictionless and painless to use as we could make it.

I want to fetch first 28 characte

相关标签:
13条回答
  • 2020-11-28 10:46

    From AlfaSky:

    function addEllipsis($string, $length, $end='…')
    {
        if (strlen($string) > $length)
        {
            $length -= strlen($end);
            $string  = substr($string, 0, $length);
            $string .= $end;
        }
    
        return $string;
    }
    

    An alternate, more featureful implementation from Elliott Brueggeman's blog:

    /**
     * trims text to a space then adds ellipses if desired
     * @param string $input text to trim
     * @param int $length in characters to trim to
     * @param bool $ellipses if ellipses (...) are to be added
     * @param bool $strip_html if html tags are to be stripped
     * @return string 
     */
    function trim_text($input, $length, $ellipses = true, $strip_html = true) {
        //strip tags, if desired
        if ($strip_html) {
            $input = strip_tags($input);
        }
    
        //no need to trim, already shorter than trim length
        if (strlen($input) <= $length) {
            return $input;
        }
    
        //find last space within length
        $last_space = strrpos(substr($input, 0, $length), ' ');
        $trimmed_text = substr($input, 0, $last_space);
    
        //add ellipses (...)
        if ($ellipses) {
            $trimmed_text .= '...';
        }
    
        return $trimmed_text;
    }
    

    (Google search: "php trim ellipses")

    0 讨论(0)
  • 2020-11-28 10:47

    you can use wordwrap.

    string wordwrap  ( string $str  [, int $width= 75  [, string $break= "\n"  [, bool $cut= false  ]]] )
    

    -

    function firstNChars($str, $n) {
      return array_shift(explode("\n", wordwrap($str, $n)));
    }
    
    echo firstNChars("bla blah long string", 25) . "...";
    

    disclaimer: didn't test it.

    additionally, if your string contains \ns, it might get broken earlier.

    0 讨论(0)
  • 2020-11-28 10:48

    why not try exploding it and getting the first 4 elements of the array?

    0 讨论(0)
  • 2020-11-28 10:51

    Here's one way you could do it:

    $str = "Stack Overflow is as frictionless and painless to use as we could make it.";
    
    $strMax = 28;
    $strTrim = ((strlen($str) < $strMax-3) ? $str : substr($str, 0, $strMax-3)."...");
    
    //or this way to trim to full words
    $strFull = ((strlen($str) < $strMax-3) ? $str : strrpos(substr($str, 0, $strMax-3),' ')."...");
    
    0 讨论(0)
  • 2020-11-28 10:53

    You can use the wordwrap() function, then explode on newline and take the first part:

    $str = wordwrap($str, 28);
    $str = explode("\n", $str);
    $str = $str[0] . '...';
    
    0 讨论(0)
  • 2020-11-28 10:54

    I would use a string tokenizer to split the string into words much like this:

    $string = "Stack Overflow is as frictionless and painless to use as we could make it.";
    $tokenized_string = strtok($string, " ");
    

    Then you can pull out the individual words any way you want.


    Edit: Greg has a much better and more elegant way of doing what you want. I would go with his wordwrap() solution.

    0 讨论(0)
提交回复
热议问题