Home>Article>Backend Development> How to solve the problem of garbled Chinese string interception in PHP
php Chinese string interception garbled solution: first create a PHP sample file; then define a "utf_substr" method; finally use "substr" and other functions to implement PHP interception of "UTF-8" strings, and Just solve the half-character problem.
Recommended: "PHP Video Tutorial"
php intercepts utf-8 Chinese string garbled Solution
/** * PHP截取UTF-8字符串,解决半字符问题。 * 英文、数字(半角)为1字节(8位),中文(全角)为2字节 * @return 取出的字符串, 当$len小于等于0时, 会返回整个字符串 * @param $str 源字符串 * $len 左边的子串的长度 */ function utf_substr($str,$len){ for($i=0;$i<$len;$i++){ $temp_str=substr($str,0,1); if(ord($temp_str) > 127){ if($i<$len){ $new_str[]=substr($str,0,3); $str=substr($str,3); } }else{ $new_str[]=substr($str,0,1); $str=substr($str,1); } } return join($new_str);}
The above is the detailed content of How to solve the problem of garbled Chinese string interception in PHP. For more information, please follow other related articles on the PHP Chinese website!