This example describes the use of the character intercept function msubstr () in thinkphp. Share to everyone for your reference, as follows:
Thinkphp has a built-in character intercept function msubstr () as follows:
Msubstr ($str, $start =0, $length, $charset = "Utf-8", $suffix =true)
$STR: the string to intercept
$start = 0: Start position, starting from 0 by default
$length: Intercept length
$charset = "Utf-8": Character encoding, default UTF-8
$suffix =true: If an ellipsis is displayed after the truncated character, the default is true to show that false is not displayed
Call the following
{$vo. title|msubstr=5,5, ' Utf-8 ', false}
Explanation: Intercept the string $vo.title, starting with the 5th character, intercept 5, encode as UTF-8, do not display the ellipsis
Recently, when writing a program with thinkphp, I encountered a call to undefined function msubstr () Such an error,
Cause Analysis:
In the foreground template used to msubstr|msubstr=###,5,5, ' utf-8 ', false this function to intercept the number of characters;
The problem arises:
Start writing locally when the Windows operating system did not report an error, and then upload the project to the Linux server when access to the front desk call to undefined function msubstr () this error;
Problem exclusion:
At the beginning of the think official saw a little buddy posted it. To define the function in a public 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;}
It can be used locally after the definition is finished, and it is also seen that a small partner says to put this function into the thinkphp core function file can solve the call to undefined function msubstr () error problem, Put in their own project common directory under the common.php said invalid, but do not want to change the core files think easy to upgrade later;
Solution:
Or Linx under the file capitalization problem, after I carefully review found that Since the project does not automatically generate common.php to be manually built, I wrote the common under the local project under the common.php C as uppercase, and then view the official core common under the common.php C is lowercase. Finally, he was changed to a lowercase problem resolution;
Record it and avoid making this kind of low-level mistake again!
It is hoped that this article is helpful to the PHP program design based on thinkphp framework.