PHP正确解析UTF-8字符串
来源:程序员人生 发布时间:2014-06-06 05:25:12 阅读次数:2492次
- $str = '今天非常Happy,所有决定去KFC吃可乐鸡翅!!!';
-
-
-
-
- function utf8sub($str,$len) {
- if($len <= 0){
- return '';
- }
-
- $offset = 0;
- $chars = 0;
- $res = '';
-
- while($chars < $len){
-
-
-
- $high = ord(substr($str,$offset,1));
-
-
- if($high == null ){
- break;
- }
- if(($high>>2) === 0x3F){
-
- $count = 6;
- }else if(($high>>3) === 0x1F){
-
- $count = 5;
- }else if(($high>>4) === 0xF){
-
-
- $count = 4;
- }else if(($high>>5) === 0x7){
-
-
- $count = 3;
- }else if(($high>>6) === 0x3){
-
- $count = 2;
- }else if(($high>>7) === 0x0){
- $count = 1;
- }
-
-
- $res .= substr($str,$offset,$count);
- $chars += 1;
- $offset += $count;
- }
- return $res;
- }
- echo utf8sub($str,100);
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠