Shorten a text string in PHP

前端 未结 9 2394
暗喜
暗喜 2020-12-20 02:32

Is there a way to trim a text string in PHP so it has a certain number of characters? For instance, if I had the string:

$string = \"this is a string\";

9条回答
  •  遥遥无期
    2020-12-20 03:37

    substr cuts words in half. Also if word contains UTF8 characters, it misbehaves. So it would be better to use mb_substr:

    $string = mb_substr('word word word word', 0, 10, 'utf8').'...';

提交回复
热议问题