短文本,PHP [英] Short Text, PHP
本文介绍了短文本,PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了这个功能:
function shorter($text, $chars_limit) {
if (strlen($text) > $chars_limit)
return substr($text, 0, strrpos(substr($text, 0, $chars_limit), " ")).'...';
else return $text;
}
如果我使用 echo short($input, 11)
它工作正常,但如果输入中有一些空格,否则输入看起来像:
and if I use echo shorter($input, 11)
it works ok, but if there are some white spaces in the input, otherwise for the input looking like:
wwwwwwwwwww
wwwwwwwwwww
函数将把它改成:
...(3 个点).
我不想把它改成这样:
www ...
你知道如何重建这个脚本吗?提前致谢.
Have you got any ideas how to rebuild this script? Thank you in advance.
推荐答案
我假设您只想输入.如果它比 X 长,则在 X 处将其截断并添加...".
Im assuming you just want to take an input. If it is longer than X then cut it off at X and add "...".
// 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;
}
}
我没有测试过这个,但它应该可以工作.:)
I didn't test this, but it should work. :)
这篇关于短文本,PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文