Thinkphp has a built-in template engine that is comparable to smarty, which brings us great convenience. The same goes for calling functions. You can call the functions you need just like smarty, and the official has built-in some commonly used functions for everyone to call.
For example, the interception string function we are talking about today can be written like this in the thinkphp template engine: {$vo.title|msubstr=0,5,'utf-8′,false} As for { $vo.title} is certainly familiar to everyone. Let’s talk about the following function msubstr. What it means is to intercept the string $vo.title, starting from 0 characters and intercepting 5 characters. UTF-8 encoding is used, and the ellipsis is not displayed after interception by default. If you want to display the ellipsis, just change false to true.
Function explanation:
msubstr($str, $start=0, $length, $charset=”utf-8″, $suffix=true)
$str: The string to be intercepted
$start=0: Starting position, starting from 0 by default
$length : Intercept length
$charset=”utf-8″: Character encoding, default UTF-8
$suffix=true: Whether it is An ellipsis is displayed after the intercepted characters. The default value is true, and false is not displayed.
ps: If it cannot be called normally, it means that you have not loaded the function library. You can use Load( 'extend'); to load the function, just put it in the action~!
After trial: The official msubstr function seems to be unable to add an ellipse anyway. I found a modification method on the official website forum, and it can be used normally after testing~!
Modify the msubstr function of the Commonextend.PHP file to the following code:
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) { if(function_exists("mb_substr")){ if($suffix) return mb_substr($str, $start, $length, $charset)."..."; else return mb_substr($str, $start, $length, $charset); } elseif(function_exists('iconv_substr')) { if($suffix) return iconv_substr($str,$start,$length,$charset)."..."; else return iconv_substr($str,$start,$length,$charset); } $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)); if($suffix) return $slice."…"; return $slice; }
Related recommendations:
Example of string interception function mb_substr in php
php string interception function usage
phpChinese and English string interception function (including html )
The above is the detailed content of Thinkphp Chinese and English string interception function displays ellipses. For more information, please follow other related articles on the PHP Chinese website!