Home  >  Article  >  Backend Development  >  Solution to php substr Chinese garbled code

Solution to php substr Chinese garbled code

高洛峰
高洛峰Original
2016-11-29 16:16:221497browse

If you directly use substr to intercept a Chinese string, it will definitely produce garbled characters, because it will divide a Chinese character into half. Why is this happening? It is because of the internal encoding problem of the machine. Now let’s look at the solution

1. Using the mbstring extension library mb_substr interception will not cause garbled characters.

2. Write the interception function yourself, but the efficiency is not as high as using the mbstring extension library.

3. If it is just to output the intercepted string, it can be implemented in the following way: substr($str, 0, 30).chr(0).

The PHP example code is as follows:

* Can avoid intercepting garbled Chinese characters

* Parameter $str is a string, $start is the start character, $len is the end character

* Returns the intercepted character

*/

function msubstr($str, $start, $len) {

$tmpstr = "";//Open source code phpfensi.com

$strlen = $start + $len;

for($i = 0; $i < $strlen; $i++) {

if(ord(substr( $str, $i, 1)) > 0xa0) {

$tmpstr .= substr($str, $i, 2);

$i++;

} else

$tmpstr .= substr($str , $i, 1);

}

return $tmpstr;

}

Method 2, PHP method to intercept Chinese text strings without garbled characters, the code is as follows:

function gbsubstr($string, $start, $ length) {

if(strlen($string)>$length){

$str=null;

$len=$start+$length;

for($i=$start;$i<$len ;$i++){

if(ord(substr($string,$i,1))>0xa0){

$str.=substr($string,$i,2);

$i++;

}else{

      $str.=substr($string,$i,1);

  }

}

  return $str.'...';

}else{

  return  $string;

}

}

?>


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