php字符是汉字还是字符,php判断字符串中是否包含中文汉字和获得字符串中的汉字...

一,判断全是中文

$str="'324是中国人";

if(!eregi("[^\x80-\xff]","$str")){

echo "全是中文";

}else{

echo "不是";

}

二,判断是否包含中文汉字

$str = "中文汉字";

if (preg_match("/[\x7f-\xff]/", $str)) {

echo "含有中文";

}else{

echo "没有中文";

}

$pattern = '/[^\x00-\x80]/';

if(preg_match($pattern,$str)){

echo "含有中文";

}else{

echo "没有中文";

}

PHP取得汉字(中文):

function getChinese($str,$charset='utf8'){

if($charset=='gb2312'){

if(!preg_match_all("/^[".chr(0xa1)."-".chr(0xff)."]+/",$str,$match)){

return false;

}

return implode('',$match[0]);

}

//

if($charset=='utf8'){

if(!preg_match_all("/[\x{4e00}-\x{9fa5}]+/u",$str,$match)){

return false;

}

return implode('',$match[0]);

}

return false;

}

说明:PHP利用正则表达式如何匹配出一段字符串中的中文,对于这个问题,网上也有很多代码,但是大部分都存在纰漏,所以下面为大家提供真正能够匹配出中文的代码。分两种编码形式:

1)utf-8的编码格式,匹配中文代码如下:

$str = "utf-8下匹配出中文字符串";

$preg = "/[\x{4e00}-\x{9fa5}]+/u";

if(preg_match_all($preg,$str,$matches)){

print_r($matches);

}

?>

2)gb2312的编码格式,匹配中文字符串代码如下:

$str = "gb2312下匹配出中文字符串";

$preg = "/([".chr(0xb0)."-".chr(0xf7)."][".chr(0xa1)."-".chr(0xfe)."])+/i";

if(preg_match($preg,$str,$matches)){

print_r($matches);

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值