Home >Backend Development >PHP Tutorial >Introducing a Chinese and English string interception function built into Thinkphp_PHP Tutorial

Introducing a Chinese and English string interception function built into Thinkphp_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:251091browse

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 must be familiar with it. 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, and the ellipsis is not displayed after interception by default. If you want to display the ellipsis, 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: starting position, starting from 0 by default
  • $length: intercept 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 to not display

ps: 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~!

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, and it can be used normally after testing~!

Modify the msubstr function of the Commonextend.php file to the following code:

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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752497.htmlTechArticleThinkphp 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...
Statement:
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