本文翻译自:Get first n characters of a string
How can I get the first n characters of a string in PHP? 如何在PHP中获取字符串的前n个字符? What's the fastest way to trim a string to a specific number of characters, and append '...' if needed? 将字符串修剪为特定字符数的最快方法是什么,如果需要,可以附加“...”?
参考:https://stackoom.com/question/DGX2/获取字符串的前n个字符
I developed a function for this use 我开发了这种用途的功能
function str_short($string,$limit)
{
$len=strlen($string);
if($len>$limit)
{
$to_sub=$len-$limit;
$crop_temp=substr($string,0,-$to_sub);
return $crop_len=$crop_temp."...";
}
else
{
return $string;
}
}
you just call the function with string and limite 你只需用字符串和限制来调用函数
eg: str_short("hahahahahah",5)
; 例如: str_short("hahahahahah",5)
;
it will cut of your string and add "..." at the end 它将剪切你的字符串并在最后添加“...”
:) :)
This is what i do 这就是我做的
function cutat($num, $tt){
if (mb_strlen($tt)>$num){
$tt=mb_substr($tt,0,$num-2).'...';
}
return $tt;
}
where $num stands for number of chars, and $tt the string for manipulation. 其中$ num表示字符数,$ tt表示操作字符串。
sometimes, you need to limit the string to the last complete word ie: you don't want the last word to be broken instead you stop with the second last word. 有时,你需要将字符串限制为最后一个完整的单词,即:你不希望最后一个单词被打破,而是用第二个单词停止。
eg: we need to limit "This is my String" to 6 chars but instead of 'This i..." we want it to be 'This..." ie we will skip that broken letters in the last word. 例如:我们需要将“This is my String”限制为6个字符,而不是“This i ...”,我们希望它为“This ...”,即我们将在最后一个单词中跳过那些破碎的字母。
phew, am bad at explaining, here is the code. 嗯,我不好解释,这是代码。
class Fun {
public function limit_text($text, $len) {
if (strlen($text) < $len) {
return $text;
}
$text_words = explode(' ', $text);
$out = null;
foreach ($text_words as $word) {
if ((strlen($word) > $len) && $out == null) {
return substr($word, 0, $len) . "...";
}
if ((strlen($out) + strlen($word)) > $len) {
return $out . "...";
}
$out.=" " . $word;
}
return $out;
}
}
To create within a function (for repeat usage) and dynamical limited length, use: 要在函数内创建(重复使用)和动态有限长度,请使用:
function string_length_cutoff($string, $limit, $subtext = '...')
{
return (strlen($string) > $limit) ? substr($string, 0, ($limit-strlen(subtext))).$subtext : $string;
}
// example usage:
echo string_length_cutoff('Michelle Lee Hammontree-Garcia', 26);
// or (for custom substitution text
echo string_length_cutoff('Michelle Lee Hammontree-Garcia', 26, '..');
If you want to cut being careful to don't split words you can do the following 如果你想小心不要分割单词,你可以做以下事情
function ellipse($str,$n_chars,$crop_str=' [...]')
{
$buff=strip_tags($str);
if(strlen($buff) > $n_chars)
{
$cut_index=strpos($buff,' ',$n_chars);
$buff=substr($buff,0,($cut_index===false? $n_chars: $cut_index+1)).$crop_str;
}
return $buff;
}
if $str is shorter than $n_chars returns it untouched. 如果$ str比$ n_chars短,则返回它不变。
If $str is equal to $n_chars returns it as is as well. 如果$ str等于$ n_chars,则返回原样。
if $str is longer than $n_chars then it looks for the next space to cut or (if no more spaces till the end) $str gets cut rudely instead at $n_chars. 如果$ str超过$ n_chars那么它会查找下一个要剪切的空间或者(如果没有更多的空格直到结束)$ str会被粗略地削减而不是$ n_chars。
NOTE: be aware that this method will remove all tags in case of HTML. 注意:请注意,如果是HTML,此方法将删除所有标记。