Home > Backend Development > PHP Tutorial > Detailed explanation of the interception string function of ThinkPHP template substr

Detailed explanation of the interception string function of ThinkPHP template substr

高洛峰
Release: 2023-03-04 18:06:01
Original
2389 people have browsed 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 ellipse after the intercepted character, the default is true to display, false is not to display

Call As explained below

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

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

I am currently writing a program with thinkphp When I encountered an error like Call to undefined function msubstr(),

Cause analysis:

used msubstr|msubstr=

,5,5,'utf in the front-end template -8',false This function is used to intercept the number of characters;

The problem arises:

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 it. The front desk reported the error Call to undefined function msubstr();

Problem Troubleshooting:

I started to see some friends posted on think official website that they 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 putting this function under the Thinkphp core function file can solve the problem of Call to undefined function msubstr() error. The common.php in the Common directory of my project says it is invalid, but I don’t want to change the core files of Think to facilitate future upgrades;

Solution:

It’s still a file case problem under Linx. After careful inspection, I found that since the project does not automatically generate common.php and 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 common.php under the official core Common. C is lowercase. Finally, the problem was solved by changing it to lowercase;

Record it to avoid making such low-level mistakes in the future!

I hope this article will be helpful to everyone’s PHP programming based on the thinkPHP framework.

For more detailed explanations of the interception string function of ThinkPHP template substr, please pay attention to 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