PHP正确解析UTF-8字符串
栏目:php教程时间:2014-06-06 05:25:12
- $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);
------分隔线----------------------------
------分隔线----------------------------