Thinkphp built-in a template engine comparable to smarty, which brought us a lot of convenience. The calling function, like smarty, can invoke the functions it needs, and the official has built in some common functions for everyone to call.
Like today we're talking about intercepting string functions, which can be written in the thinkphp template engine: {$vo. title|msubstr=0,5, ' Utf-8′,false} as for {$vo. title} This is certainly not a stranger. Let's talk about the function behind the msubstr. It means intercepting the string $vo.title, starting from 0 characters and intercepting 5 characters. Using the UTF-8 encoding, the default interception after the ellipsis is not displayed, if you want to display the ellipsis, directly false to true can be.
Function Explanation:
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
Note: If it is not normal to call, it means that you do not load the library, you can use load (' extend '), to load the function, put it in the action can be!
Note: The method of extending the library cannot be used directly, it needs to be loaded or copied into the Project function library.
To load an extension function library, using:
Load (' extend ');
Once you have loaded the Extension function library, you can call all of them.
function index () {
Load (' extend '); Here here!
if ($_post[' password ']!=$_post[' Repassword ')} {
$this->error (' two times password inconsistent ');
$user =d (' user ');
if ($vo = $user->create ()) {
......
}
After trial: The official Msubstr function seems to add no ellipsis, the official website of the forum to find a way to modify, after testing can be normal use ~! The MSUBSTR function for modifying the common\extend.php file is the following code:
PHP code
/**
+----------------------------------------------------------
* String interception, support for Chinese and other encodings
+----------------------------------------------------------
* @static
* @access Public
+----------------------------------------------------------
* @param string $STR strings that need to be converted
* @param string $start start position
* @param string $length intercept length
* @param string $charset encoded 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);
}
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;
}