Limit String Length

后端 未结 10 1224
不思量自难忘°
不思量自难忘° 2020-12-01 03:56

I\'m looking for a way to limit a string in php and add on ... at the end if the string was too long.

10条回答
  •  抹茶落季
    2020-12-01 04:46

    In Laravel, there is a string util function for this, and it is implemented this way:

    public static function limit($value, $limit = 100, $end = '...')
    {
        if (mb_strwidth($value, 'UTF-8') <= $limit) {
            return $value;
        }
    
        return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
    }
    

提交回复
热议问题