The example in this article describes the usage of the character interception function msubstr() in thinkphp. Share it with everyone for your reference, the details are as follows:
ThinkPHP has a built-in character interception function msubstr() as follows:
msubstr($str, $start=0, $length, $charset= "utf-8", $suffix=true)
$str: the string to be intercepted
$start=0: starting position, default starts from 0
$length: interception length
$charset="utf-8": Character encoding, default UTF-8
$suffix=true: Whether to display an ellipse after the intercepted character, the default is true to display, false is not to display
Call As explained below
{$vo.title|msubstr=5,5,'utf-8',false}
: intercept the string $vo.title, starting from the 5th character, intercept 5 characters, encode them as UTF-8, and do not display the ellipses
I am currently writing a program with thinkphp When I encountered an error like Call to undefined function msubstr(),
Cause analysis:
used msubstr|msubstr=
,5,5,'utf in the front-end template -8',false This function is used to intercept the number of characters; The problem arises: When I started writing locally, the windows operating system did not report an error. Later, when I uploaded the project to the Linux server, I accessed it. The front desk reported the error Call to undefined function msubstr();Problem Troubleshooting:I started to see some friends posted on think official website that they need to define this function under the public function library; The function is as follows:function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=false){ 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; }