我得到了这个功能:
function shorter($text, $chars_limit) {
if (strlen($text) > $chars_limit)
return substr($text, 0, strrpos(substr($text, 0, $chars_limit), " ")).'...';
else return $text;
}
如果我使用echo shorter($input, 11)
它工作正常,但如果输入中有一些空格,否则输入看起来像:
wwwwwwwwwwwwwww
该函数会将其更改为:
...(3 点)。
我不希望它变成这样:
www ...
您对如何重建此脚本有任何想法吗?
先感谢您。
我假设您只想接受输入。如果它比X长,则在X处将其切断并添加“...”。
// Start function
function shorter($text, $chars_limit)
{
// Check if length is larger than the character limit
if (strlen($text) > $chars_limit)
{
// If so, cut the string at the character limit
$new_text = substr($text, 0, $chars_limit);
// Trim off white space
$new_text = trim($new_text);
// Add at end of text ...
return $new_text . "...";
}
// If not just return the text as is
else
{
return $text;
}
}
我没有测试这个,但它应该有效。 :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)