检测中文长度gbk下2个字节
//$str = 'fff&sdf你是sdf好fdf啊b歌hello中world';
$str = 'd你b_fff是好啊歌中潺潺 ';
echo chineselength($str)."-----<br>";
function chineselength($str){
$s=iconv("utf-8","gbk",$str);
//echo $s;die();
//$int=preg_match_all("/[\x{4e00}-\x{9fa5}]+/u", $s, $result);
$count=0;
$int=preg_match_all("/[".chr(0xa1)."-".chr(0xff)."]+/", $s, $result);
foreach($result[0] as $k=>$v){
$count+=mb_strlen($v,"gbk");
}
return $count;
}
======
发现chr有时不好使,可以使用下边的
if(preg_match('/^[\x7f-\xff]+$/', $str)){ return true;//全是中文 }else{ return false;//不全是中文 } }
$str = 'B、比较低的楼层,可以利用结实的绳索(也可用床单、窗帘布等物撕成条拧成绳)拴好绳索,沿绳爬下'; $rs = preg_match('/(\P{Han}+)/',$str,$match); 引用:https://segmentfault.com/q/1010000000695368/a-1020000000695465 http://php.net/manual/en/regexp.reference.unicode.php