找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 726|回复: 0
打印 上一主题 下一主题

ThinkPHP 模板substr的截取字符串函数详解

[复制链接]

3444

主题

3465

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11142
跳转到指定楼层
楼主
发表于 2018-2-14 05:28:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            ThinkPHP 模板substr的截取字符串函数
在Common/function.php加上以下代码
/**
** 截取中文字符串
**/
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){
if(function_exists("mb_substr")){
$slice= mb_substr($str, $start, $length, $charset);
}elseif(function_exists('iconv_substr')) {
$slice= iconv_substr($str,$start,$length,$charset);
}else{
$re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
$re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
$re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
$re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
}
$fix='';
if(strlen($slice)
前端页面需要截取字符串时
{$v.title|msubstr=0,5}
/****************************案例****************************/
//新闻列表
public function NewsList(){
$this->assign('title','news');
$p = I('page',1);
$listRows = 10;
$News = M('news');
$info = $News->field('id,title,subtitle,publish_date,img,content')->where(array('type'=>'news','status'=>'1'))->order('flag desc,sort_no desc')->page($p,$listRows)->select();
$this->assign('news',$info);
$count = $News->where(array('type'=>'news','status'=>'1'))->count();
$Page = new Page($count,$listRows);
$show = $Page->show();
$this->assign('page',$show);
//var_dump($info);
$this->display();
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
            
            
您可能感兴趣的文章:
  • Thinkphp模板中截取字符串函数简介
  • ThinkPHP的截取字符串函数无法显示省略号的解决方法
  • thinkPHP内置字符串截取函数用法详解
  • THINKPHP截取中文字符串函数实例代码
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端