ThinkPHP 3.1.3貌似没有内置的截取中文字符串的方法,找了半天没找到,下面作者自己加了一个截取中文字符串的函数,具体代码如下,有需要的朋友可以参考下。
以下代码加在项目所在目录的Common目录下的common.php文件里面的,比如作者的就是www/Common/common.php文件,当然你也可以直接加到thinkphp的Common/common.php文件里面,这样就所有的项目都可以使用了。
function truncate_cn($string,$length=0,$ellipsis='…',$start=0){ $string=strip_tags($string); $string=preg_replace('/\n/is','',$string); //$string=preg_replace('/ | /is','',$string);//清除字符串中的空格 $string=preg_replace('/ /is','',$string); preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/",$string,$string); if(is_array($string)&&!empty($string[0])){ $string=implode('',$string[0]); if(strlen($string)<$start+1){ return ''; } preg_match_all("/./su",$string,$ar); $string2=''; $tstr=''; //www.phpernote.com for($i=0;isset($ar[0][$i]);$i++){ if(strlen($tstr)<$start){ $tstr.=$ar[0][$i]; }else{ if(strlen($string2)<$length+strlen($ar[0][$i])){ $string2.=$ar[0][$i]; }else{ break; } } } return $string==$string2?$string2:$string2.$ellipsis; }else{ $string=''; } return $string; }
在thinkphp的模板中用法如下:
{$info.subject|truncate_cn=40,'',0}
意思是截取$info['subject']字符串,从第0个字符串开始截取长度为40的字符串,截取字符串长度小于原字符串长度的将什么都不显示,默认是...的,其实后面两个参数是可选的。
您可能感兴趣的文章
- javascript实现截取字符串功能总结(包括使用Js截取中文字符的介绍)
- thinkphp模板中判断volist循环的最后一条记录
- php性能优化:使用 isset()判断字符串长度速度比strlen()更快
- js限制只能输入英文字母和数字,不能输入中文和其他特殊字符的办法
- thinkphp自动验证与自动填充无效的解决办法
- php提取字符串中的数字
- 如何去除codeIgniter开发的网站url里面的index.php字符串
- php判断字符串是否全英文,纯中文,中英文组合的方法

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.