Home > Backend Development > PHP Tutorial > Usage analysis of character interception function msubstr() in thinkphp

Usage analysis of character interception function msubstr() in thinkphp

PHP中文网
Release: 2016-07-29 09:07:10
Original
887 people have browsed it

This article mainly introduces the usage of the character interception function msubstr() in thinkphp. It analyzes the function, definition, usage skills and precautions of the character interception function msubstr() in thinkPHP in detail in the form of examples. Friends in need can refer to it

The example in this article describes the usage of the character interception function msubstr() in thinkphp. Share it with everyone for your reference, the details are as follows:

ThinkPHP has a built-in character interception function msubstr() as follows:

msubstr($str, $start=0, $length, $charset="utf-8", $ suffix=true)

$str: the 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 ellipsis after the intercepted character, the default is true to display, false to not display

The call is as follows

The code is as follows:

{$vo.title|msubstr=5,5,'utf-8',false}
Copy after login

Explanation: Intercept the string $vo. title, starting from the 5th character, intercept 5 characters, encode them as UTF-8, and do not display the ellipses

Recently, when writing a program with thinkphp, I encountered an error like Call to undefined function msubstr(),

Cause analysis :

The function msubstr|msubstr=###,5,5,'utf-8',false is used in the front-end template to intercept the number of characters;

The problem arises:

Start writing locally At that time, the windows operating system did not report an error. Later, when I uploaded the project to the Linux server, I accessed the front desk and reported the error Call to undefined function msubstr();

Problem Troubleshooting:

I started to see friends posting on think official website When it comes out, you need to define this function under the public function 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;
}
Copy after login

After it is defined, it can be used locally; I also saw some friends saying that this function should be placed under the Thinkphp core function file. To solve the error problem of Call to undefined function msubstr(), it says that it is invalid when placed under common.php in the Common directory of your project, but you don’t want to change the core file of Think to facilitate future upgrades;

Solution:

Still under Linx There is a file case problem. After careful inspection, I found that since the project does not automatically generate common.php, it needs to be built manually. It turns out that I wrote the C in common.php under Common under the local project in uppercase, and then checked the official core Common The C under common.php is lowercase. Finally, the problem was solved by changing it to lowercase;

Record it to avoid making such stupid mistakes in the future!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template