如何剪切字符串并添加悬浮标记?
问题描述:
I wan to cut a string in around 300 characters and add "..." at the end if 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
我要剪切一个大约300个字符的字符串,如果它在上面则添加“...” 那个字符数。 我知道这不是很难,但我不想把一个字切成两半,所以我想知道我该怎么做,所以它最终不会像:“那鸟突然......” / p>
谢谢 p> div>
答
$str = 'A very long string here';
$str = wordwrap($str, 100);
$str = explode("
", $str);
$str = $str[0] . '...';
答
function limit($str, $limit, $append = '...') {
return preg_replace('/\S*$/', '', mb_substr($str, 0, $limit)) . $append;
}