Home > Article > Backend Development > Thinkphp built-in interception string function
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 string interception 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}, everyone It’s definitely no stranger. 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. By default, ellipses are not displayed after interception. If you want to display ellipses, 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: start Position, starting from 0 by default
$length: intercepted length
$charset=”utf-8″: character encoding, default UTF-8
$suffix=true: whether to display an ellipsis after the intercepted character, default true display, false Not displayed
Note: 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 and put it in the action~!
Note: The method of extending the function library cannot be used directly. It needs to be loaded or copied into the project function library before it can be used.
To load the extended function library, use:
Load('extend');
After loading the extended function library, you can call all functions in it.
function index(){
Load('extend'); //here here!
if($_POST['password']!=$_POST['repassword']){
$this->error('Two passwords are inconsistent');
$user=D('user');
if ($vo=$user->create()){
......
}
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. The test can be used normally~! Modify the msubstr function of the Commonextend.php file to the following code:
Php code
/**
+-------------------------------------------------- ----------
* String interception, supports Chinese and other encodings
+------------------------ ----------------------------------
* @static
* @access public
+-- -------------------------------------------------- ------
* @param string $str The string that needs to be converted
* @param string $start Starting position
* @param string $length Interception length
* @param string $charset Encoding format
* @param string $suffix truncate display characters
+---------------------------------------- --------------------------
* @return string
+-------------------------- ----------------------------------------
*/
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); return iconv_substr($str,$start,$length,$charset)."...";
. ['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;
}