Home > Backend Development > PHP Tutorial > About thinkPHP's built-in string interception function

About thinkPHP's built-in string interception function

不言
Release: 2023-03-30 19:54:01
Original
2260 people have browsed it

This article mainly introduces the usage of thinkPHP's built-in string interception function. It analyzes the function and specific usage of thinkPHP's built-in string interception function in the form of examples, as well as the source code modification techniques for unable to add ellipses. Friends in need can refer to it. Next

The example in this article describes the usage of thinkPHP's built-in string interception function. Share it with everyone for your reference, the details are as follows:

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 this. 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)
Copy after login

## Parameter function:

$str: 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 to not display

ps: If not If it is called normally, it means that you have not loaded the function library. You can use Load('extend'); to load the function and just 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 Common\extend.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;
}
Copy after login

The above is this article The entire content, I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the table method of ThinkPHP CURD method

Use ThinkPHP’s built-in ThinkAjax to implement asynchronous transmission technology Implementation

The above is the detailed content of About thinkPHP's built-in string interception function. For more information, please follow other related articles on the PHP Chinese website!

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