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-12 09:01:09
Original
842 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 based on examples. Friends in need can refer to it. The following

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, starting from 0 by default
$ 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

is called 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 ellipsis

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 Number of characters;

The problem occurred:

When I started writing locally, 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 a Call. to undefined function msubstr() error;

Problem Troubleshooting:

I started to see on think official website that some friends have posted that they need to be defined under the public function library This function; 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 they should put this function into Thinkphp The error problem of Call to undefined function msubstr() can be solved in the core function file. If you put it under common.php in the Common directory of your project, it says it is invalid, but you don’t want to change the core file of Think to facilitate future upgrades;

Solution:

It is still a file case problem under Linx. After careful inspection, I found that since the project does not automatically generate common.php, I have to build it manually. It turns out that I have Common in the local project. The C in common.php under common.php is written in uppercase, and then check the common.php under the official core Common. The C is in lowercase. Finally, the problem was solved by changing it to lowercase;

Record it to avoid making such low-level 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