Home  >  Article  >  PHP Framework  >  msubstr() character interception function in thinkphp

msubstr() character interception function in thinkphp

PHPz
PHPzforward
2020-09-25 16:01:152432browse

msubstr() character interception function in thinkphp

Recommended related tutorials: thinkphp framework tutorial

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: 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

The call is as follows

{$vo.title|msubstr=5,5,’utf-8′,false}

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

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

Cause analysis:

msubstr|msubstr=

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

The problem occurred:

When I started writing locally, the windows operating system did not report an error. Later, I When the project is uploaded to the Linux server, the error Call to undefined function msubstr() is reported when accessing the front desk;

Problem Troubleshooting:

I started to see some friends on think official website We posted that we 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;
 
}

After being defined, it can be used locally; I also saw some friends saying that this function should be placed in the Thinkphp core function file You can solve the problem of Call to undefined function msubstr() error. 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 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, it needs to be built manually. It turns out that I created common under Common under the local project. The C in .php is written in uppercase, and then check common.php under the official core Common. The 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!

For more programming-related knowledge, please visit: Introduction to Programming

! ! ###

The above is the detailed content of msubstr() character interception function in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete