获取字符串的前n个字符

本文翻译自: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? 将字符串修剪为特定字符数的最快方法是什么,如果需要,可以附加“...”?


#1楼

参考:https://stackoom.com/question/DGX2/获取字符串的前n个字符


#2楼

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 它将剪切你的字符串并在最后添加“...”
:) :)


#3楼

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表示操作字符串。


#4楼

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;
    }

}

#5楼

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, '..');

#6楼

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,此方法将删除所有标记。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值