How to truncate a string in PHP to the sentence closest to a certain number of characters?

后端 未结 4 1718
余生分开走
余生分开走 2020-12-20 20:38

I want to truncate/shorten my string to the sentence closest to a ceratain number of characters.

I have a working function, but my function truncate to the word clos

4条回答
  •  余生分开走
    2020-12-20 21:05

    I simply use this method and it works as required!

    This way no new functions are created and the code is clean and simple.

    $article = strlen($article) > $maxlength ? substr($article, 0, $maxlength) : $article;

提交回复
热议问题