php 正则匹配获取汉字
随风飘扬 php 2016年03月01日3:39 2486
我这个测试了一下,支持GB2312编码和UTF-8编码,其他编码下思路类似,只是要找下编码的起始值就
我这个测试了一下,支持GB2312编码和UTF-8编码,其他编码下思路类似,只是要找下编码的起始值就可以了.
function match_chinese($chars,$encoding='utf8'){
$pattern =($encoding=='utf8')?'/[\x{4e00}-\x{9fa5}]/u':'/[\x80-\xFF]/';
preg_match_all($pattern,$chars,$result);
$temp =join('',$result[0]);
return $temp;
}
调用:
$str ="中文汉字English Chars数字12131321";
//UTF-8编码下
echo match_chinese($str);
//gb2312编码下
echo match_chinese($str,'gb2312');
Statement: all articles / drawings reproduced on this website are for the purpose of transmitting more information and facilitating discussion, which does not mean that the website, the host and the organizer agree with their opinions or confirm the authenticity of their contents. The contents of the articles are for reference only. If the rights of the original author are violated, please contact the website administrator.
Comment list
-
No data