Home >Backend Development >PHP Tutorial >Solve the problem of intercepting Chinese strings with PHP
If PHP intercepts a string in English, it is easy. Just use substr directly, and generally there will be no garbled characters. Chinese is a bit troublesome.
Two solutions are given below:
(1) Directly use the Multi-Byte function Just use the mb_substr(); function of the library. The example code is as follows
<?php echo mb_substr($str, $start, $length, $encoding); echo "<br />"; ?>
This method is simple, fast, safe, beautiful, and tempting. . . Anyway, any good words can be used to describe it, but unfortunately my GoDaddy host does not support it, so I have to find another way out
(2) Customize the Chinese string interception function, basically just write a function imitating mb_substr and call it directly, as follows The code I found for me is actually very simple
<?php function substr_cn($string_input,$start,$length) { /* 功能: * 此算法用于截取中文字符串 * 函数以单个完整字符为单位进行截取,即一个英文字符和一个中文字符均表示一个单位长度 * 参数: * 参数$string为要截取的字符串, * 参数$start为欲截取的起始位置, * 参数$length为要截取的字符个数(一个汉字或英文字符都算一个) * 返回值: * 返回截取结果字符串 * */ $str_input=$string_input; $len=$length; $return_str=""; //定义空字符串 for ($i=0;$i<2*$len+2;$i++) $return_str=$return_str." "; $start_index=0; //计算起始字节偏移量 for ($i=0;$i<$start;$i++) { if (ord($str_input{$start_index}>=161)) //是汉语 { $start_index+=2; } else //是英文 { $start_index+=1; } } $chr_index=$start_index; //截取 for ($i=0;$i<$len;$i++) { $asc=ord($str_input{$chr_index}); if ($asc>=161) { $return_str{$i}=chr($asc); $return_str{$i+1}=chr(ord($str_input{$chr_index+1})); $len+=1; //结束条件加1 $i++; //位置偏移量加1 $chr_index+=2; continue; } else { $return_str{$i}=chr($asc); $chr_index+=1; } } return trim($return_str); }//en