PHP utf-8中文截取无乱码(字符串判断版本)

/*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);

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值