PHP utf-8中文截取无乱码(字符串判断版本)
2015-01-26 14:24
393 查看
/*E4 B8 AD E5 9B BD 中国 1110 0100 10111000 10101101 1110 0101 1001101110111101 61 a 0011 1101 0xxx xxxx ,1个字节 110xx xxxx , 2个字节 1110 xxxx, 3 1111 0xxxx 4... */<pre name="code" class="php">$str = '中华人aaa民共b和国,c万岁'; function utf8sub($str,$len){ if($len <=0) return ''; $length = strlen($str); $offset=0; $chars=0; $res=''; while($chars<$len && $offset<$length){ $heigh = decbin(ord(substr($str, $offset,1))); echo $heigh ,'</br>'; if(strlen($heigh)<8){ $count=1; }else if(substr($heigh,0,3) == '110'){ $count=2; }else if(substr($heigh,0,4) == '1110'){ $count=3; }else if(substr($heigh,0,5) == '11110'){ $count=4; }else if(substr($heigh,0,6) == '111110'){ $count=5; }else if(substr($heigh,0,7) == '1111110'){ $count=6; } $res .= substr($str, $offset,$count); $chars += 1; $offset += $count; } return $res; } echo utf8sub($str,10);
相关文章推荐
- PHP字符串截取函数(解决中文乱码问题!支持UTF-8和GB2312)
- 用php截取utf-8中文字符串乱码的解决方法
- php截取utf-8中文字符串乱码的解决方法
- php截取utf-8中文字符串乱码的解决方法
- php字符串截取函数(支持中文 utf-8截取)
- PHP中的字符串截取函数,取子符串,以保证中文不出现乱码
- php截取中文字符串解决乱码问题
- 分析substr来截取UTF-8中文字符串出现乱码问题
- 用PHP来截取中文字符串而不出现乱码的实现方法
- PHP 截取中文字符串乱码问题
- PHP 中文字符串截取函数(UTF-8)有效
- PHP 截取中文字符串 UTF-8
- php中文字符串截取乱码问题解决
- [PHP] - 中文字符串截取(UTF-8与非UTF-8)
- PHP中文字符串截取无乱码方法,php汉字截取函数_preg_split()
- PHP utf-8中文截取无乱码
- PHP截取中文字符串乱码问题(UTF8编码下)
- PHP UTF-8字符串截断函数实现中文字符的无乱码截断
- php截取中文字符串无乱码的函数(主函数:ord()、substr())
- 解析使用substr截取UTF-8中文字符串出现乱码的问题