欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Laravel与CI框架中截取字符串函数

Laravel:
1
2
3
4
5
6
7
8
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;
}

Ci:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function word_limiter($str, $limit = 100, $end_char = '…')
{
  if (trim($str) === '')
  {
    return $str;
  }
 
  preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
 
  if (strlen($str) === strlen($matches[0]))
  {
    $end_char = '';
  }
 
  return rtrim($matches[0]).$end_char;
}

来自 http://www.jb51.net/article/83762.htm
普通分类: