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

前端 未结 6 1093
甜味超标
甜味超标 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:41

    // Trim very long text to 120 characters. Add an ellipsis if the text is trimmed.
    if(strlen($very_long_text) > 120) {
      $matches = array();
      preg_match("/^(.{1,120})[\s]/i", $very_long_text, $matches);
      $trimmed_text = $matches[0]. '...';
    }
    

提交回复
热议问题