开发者

How do I cut a string and add suspension marks?

开发者 https://www.devze.com 2023-02-06 01:44 出处:网络
I wan to cut a string in around 300 characters and add \"...\" at the end if开发者_如何学C it was above that number of characters. I know it can\'t be very hard but I don\'t want to cut a word in half

I wan to cut a string in around 300 characters and add "..." at the end if开发者_如何学C it was above that number of characters. I know it can't be very hard but I don't want to cut a word in half so I wanted to know how do I do it so it doesn't end up like: "And the bird suddenl..."

Thanks


http://php.net/wordwrap

$str = 'A very long string here';
$str = wordwrap($str, 100);
$str = explode("\n", $str);
$str = $str[0] . '...';


function limit($str, $limit, $append = '...') {
    return preg_replace('/\S*$/', '', mb_substr($str, 0, $limit)) . $append;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消