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

yipeiwu_com4年前PHP代码库

Laravel:

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:

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

相关文章

php获取数组中重复数据的两种方法

(1)利用php提供的函数,array_unique和array_diff_assoc来实现 复制代码 代码如下: <?php function FetchRepeatMember...

使用zend studio for eclipse不能激活代码提示功能的解决办法

其实这是项目没有经过zend studio for eclipse 编译(应该是建立索引吧)导致的,那么就只要让它重新编译项目代码即可。 操作如下: 随便新建一个项目,比如test。然后...

php中模拟POST传递数据的两种方法分享

方法1 复制代码 代码如下: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://192.168.1.135/turntab...

php include的妙用,实现路径加密

1、中转程序include.inc 复制代码 代码如下: <? include_once 'include/Base.php'; $path = ''; $url = isBase...

基于PHP文件操作的详解

知识点简介:1.判断文件或目录是否存在bool复制代码 代码如下:file_exists(string filename)  2.取得文件名复制代码 代码如下:basename...