Home > Backend Development > PHP Tutorial > How to solve the php substr garbled problem

How to solve the php substr garbled problem

小云云
Release: 2023-03-17 11:02:02
Original
3700 people have browsed it

php substr is a functional function developed based on the php language that can return a part of a string. Sometimes when we use php substr, some garbled characters will appear. How to solve it?

string substr ( string $string , int $start [, int $length ] )
Copy after login

Returns a string with a length of length starting from the start position in the string

The substr function intercepts characters by bytes. Chinese characters are 2 characters when encoded in GB2312 section, UTF-8 encoding is 3 bytes, so if Chinese characters are truncated when intercepting a string of specified length, the returned result will be garbled when displayed.

After checking, the solutions can be summarized into two categories:

1. Use the mb_substr() function instead

string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )
Copy after login

is similar to the substr() function, except Counting is based on the number of characters to ensure character safety

Using the mb_substr() function can ensure that there will be no garbled characters, but the disadvantage is that the length statistics become the number of characters instead of the number of bytes. When used for display, there will be a large difference in display length between Chinese results and English results of the same length.

2. Self-built function to enhance substr function

Here is a function that can better solve the problem of substr encountering Chinese characters. Chinese characters are calculated in 2 length units, so that the final display length of the string interception results in a mixed Chinese and English environment is close; the last incomplete character is discarded to ensure that there will be no garbled characters on the display; and it is compatible with UTF commonly used for Chinese characters -8 encoding and GB2312 encoding have good versatility.

View source code

  function getstr($string, $length, $encoding  = 'utf-8') {    
 $string = trim($string);    
 if($length && strlen($string) > $length) {    
  //截断字符    
  $wordscut = '';    
  if(strtolower($encoding) == 'utf-8') {    
  //utf8编码    
   $n = 0;    
  $tn = 0;    
 $noc = 0;    
 while ($n < strlen($string)) {    
  $t = ord($string[$n]);    
 if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {    
 $tn = 1;    
 $n++;    
  $noc++;    
 } elseif(194 <= $t && $t <= 223) {    
 $tn = 2;    
 $n += 2;    
} elseif(224 <= $t && $t < 239) {    
  $tn = 3;    
  $n += 3;    
  $noc += 2;    
 } elseif(240 <= $t && $t <= 247) {    
 $tn = 4;    
 $n += 4;    
 $noc += 2;    
 } elseif(248 <= $t && $t <= 251) {    
  $tn = 5;    
$n += 5;    
   $noc += 2;    
 } elseif($t == 252 || $t == 253) {    
 $tn = 6;    
   $n += 6;    
   $noc += 2;    
  } else {    
 $n++;    
}    
 if ($noc >= $length) {    
break;    
 }    
}    
 if ($noc > $length) {    
$n -= $tn;    
  }    
 $wordscut = substr($string, 0, $n);    
 } else {    
 for($i = 0; $i < $length - 1; $i++) {    
  if(ord($string[$i]) > 127) {    
$wordscut .= $string[$i].$string[$i + 1];    
$i++;    
 } else {    
   $wordscut .= $string[$i];    
  }    
 }    
  }    
$string = $wordscut;    
 }    
return trim($string);    
 }    
 // 示例    
 echo getstr("0一二三四五六七",1).&#39;<br />&#39;;  // 0    
 echo getstr("0一二三四五六七",2).&#39;<br />&#39;;  // 0    
echo getstr("0一二三四五六七",3).&#39;<br />&#39;;  // 0一    
 echo getstr("0一二三四五六七",4).&#39;<br />&#39;;  // 0一    
 echo getstr("0一二三四五六七",5).&#39;<br />&#39;;  // 0一二    
 echo getstr("0一a二b三四五六七",1).&#39;<br />&#39;;    // 0    
  echo getstr("0一a二b三四五六七",2).&#39;<br />&#39;;    // 0    
 echo getstr("0一a二b三四五六七",3).&#39;<br />&#39;;    // 0一    
 echo getstr("0一a二b三四五六七",4).&#39;<br />&#39;;    // 0一a    
  echo getstr("0一a二b三四五六七",5).&#39;<br />&#39;;    // 0一a
Copy after login

This function is modified from the getstr() function in UCHome 1.5.

The above is the solution on how to solve the problem of php substr garbled characters. We have also shared a series of articles about the php substr function before. If you need it, you can pay attention to the PHP Chinese website.

Related recommendations:

Several programs about PHP substr() function

php substr() function processing Chinese detailed explanation

php substr() function string interception usage example explanation


The above is the detailed content of How to solve the php substr garbled problem. 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