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

后端 未结 13 1333
再見小時候
再見小時候 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: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] . '...';
    

提交回复
热议问题