Home > Backend Development > PHP Tutorial > Thinkphp built-in interception string function

Thinkphp built-in interception string function

巴扎黑
Release: 2016-11-24 14:30:20
Original
1174 people have browsed it

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;  

}


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template