php 截取中英文混合字符串的方法
php 截取中应为字符串,就不必再用substr或者mb_substr
//截取想这样的字符串 a李三
利用ASCII
/** * * 中英混合的字符串截取 * @param unknown_type $sourcestr * @param unknown_type $cutlength */ function assoc_substr($sourcestr, $cutlength) { $returnstr = ''; $i = 0; $n = 0; $str_length = strlen ( $sourcestr ); //字符串的字节数 while ( ($n < $cutlength) and ($i <= $str_length) ) { $temp_str = substr ( $sourcestr, $i, 1 ); $ascnum = Ord ( $temp_str ); //得到字符串中第$i位字符的ascii码 if ($ascnum >= 224) {//如果ASCII位高与224, $returnstr = $returnstr . substr ( $sourcestr, $i, 3 ); //根据UTF-8编码规范,将3个连续的字符计为单个字符 $i = $i + 3; //实际Byte计为3 $n ++; //字串长度计1 } elseif ($ascnum >= 192){ //如果ASCII位高与192, $returnstr = $returnstr . substr ( $sourcestr, $i, 2 ); //根据UTF-8编码规范,将2个连续的字符计为单个字符 $i = $i + 2; //实际Byte计为2 $n ++; //字串长度计1 } elseif ($ascnum >= 65 && $ascnum <= 90) {//如果是大写字母, $returnstr = $returnstr . substr ( $sourcestr, $i, 1 ); $i = $i + 1; //实际的Byte数仍计1个 $n ++; //但考虑整体美观,大写字母计成一个高位字符 }elseif ($ascnum >= 97 && $ascnum <= 122) { $returnstr = $returnstr . substr ( $sourcestr, $i, 1 ); $i = $i + 1; //实际的Byte数仍计1个 $n ++; //但考虑整体美观,大写字母计成一个高位字符 } else {//其他情况下,半角标点符号, $returnstr = $returnstr . substr ( $sourcestr, $i, 1 ); $i = $i + 1; $n = $n + 0.5; } } return $returnstr; }
总结
相关推荐
amberom 2020-08-03
缘起宇轩阁 2019-12-21
缘起宇轩阁 2019-10-28
zhangkala 2013-06-08
spinachcqb 2019-06-27
会写code的凳子哥 2013-11-21
有梦就能实现 2020-06-10
xcguoyu 2019-10-29
amberom 2019-10-25
yangguangdblu 2019-06-18
drise 2013-11-25
wqzbxh 2012-03-23
xianzhe 2019-06-30
技术渣 2016-10-07
Phplayers 2019-06-28
wangdoudou0 2015-07-07
静心斋 2013-08-23
技术渣 2012-08-07
会写code的凳子哥 2012-07-06