Trimming a block of text to the nearest word when a certain character limit is reached?

前端 未结 6 1095
甜味超标
甜味超标 2020-12-12 05:08

Here is the question: How would your trim a block of text to the nearest word when a certain amount of characters have past. I\'m not trying to limit a certain number words

6条回答
  •  遥遥无期
    2020-12-12 05:40

    See the wordwrap function.

    I would probably do something like:

    function wrap($string) {
      $wstring = explode("\n", wordwrap($string, 27, "\n") );
      return $wstring[0];
    }
    

    (If your strings already span across severeal lines, use other char - or pattern - for the split other than "\n")

提交回复
热议问题