"; if (preg_match(" ^[" chr(0xa1) "-&quo"> php判断是否为中文正则表达式大全 - php - PHP开发网 ag试玩,ag龙虎|注册,官方ag游戏|优惠
首页 > 网络编程 > php > 正文

php判断是否为中文正则表达式大全
2014-05-09 15:10:36???来源:???评论:0 点击:

"; if (preg_match(" ^[" chr(0xa1) "-&quo


$str="aaa";
if(!eregi("[^\x80-\xff]","$str"))
{
echo "是";
}
else
{
echo "不是";
}
?>


$str = "中国";
echo $str;
echo "


";

//if (preg_match("/^[".chr(0xa1)."-".chr(0xff)."]+$/", $str)) { //只能在GB2312情况下使用
if (preg_match("/^[\x7f-\xff]+$/", $str)) { //兼容gb2312,utf-8
echo "正确输入";
} else {
echo "错误输入";
}
?>


如果想判断一个字符串内是否有含有中文,请用下面的代码:

if (preg_match("/[\x7f-\xff]/", $string)) {
echo "有中文";
}else{
echo "没有中文";
}

判断中文和编码有关 gbk是双字节,utf8是三字节,可以根据中文的范围来判断???

?

编码范围1. GBK (GB2312/GB18030)???
\x00-\xff GBK双字节编码范围???
\x20-\x7f ASCII???
\xa1-\xff 中文???
\x80-\xff 中文??
??
2. UTF-8 (Unicode)???
\u4e00-\u9fa5 (中文)???
\x3130-\x318F (韩文???
\xAC00-\xD7A3 (韩文)???
\u0800-\u4e00 (日文)???
ps: 韩文是大于[\u9fa5]的字符???
正则例子:???
preg_replace(”/([\x80-\xff])/”,”",$str);??
preg_replace(”/([u4e00-u9fa5])/”,”",$str);???

?

?

  1. //判断内容里有没有中文-GBK?(PHP)????????
  2. function?check_is_chinese($s){????????
  3. ?????return?preg_match('/[\x80-\xff]./',?$s);????????
  4. }????????
  5. ??????
  6. //获取字符串长度-GBK?(PHP)????????
  7. function?gb_strlen($str){????????
  8. ?????$count?=?0;????????
  9. ?????for($i=0;?$i<strlen($str);?$i++){????????
  10. ?????????$s?=?substr($str,?$i,?1);????????
  11. ?????????if?(preg_match("/[\x80-\xff]/",?$s))?++$i;????????
  12. ???????????++$count;????????
  13. ?????}????????
  14. ?????return?$count;????????
  15. }????????
  16. ??????
  17. //截取字符串字串-GBK?(PHP)????????
  18. function?gb_substr($str,?$len){????????
  19. ?????$count?=?0;????????
  20. ?????for($i=0;?$i<strlen($str);?$i++){????????
  21. ?????????if($count?==?$len)?break;????????
  22. ?????????if(preg_match("/[\x80-\xff]/",?substr($str,?$i,?1)))?++$i;????????
  23. ???????????++$count;????????????????
  24. ?????}????????
  25. ?????return?substr($str,?0,?$i);????????
  26. }????????
  27. ??????
  28. //统计字符串长度-UTF8?(PHP)????????
  29. function?utf8_strlen($str)?{????????
  30. ?????$count?=?0;????????
  31. ?????for($i?=?0;?$i?strlen($str);?$i++){????????
  32. ?????????$value?=?ord($str[$i]);????????
  33. ?????????if($value?>?127)?{????????
  34. ?????????????$count++;????????
  35. ?????????????if($value?>=?192?&&?$value?<=?223)?$i++;????????
  36. ?????????????elseif($value?>=?224?&&?$value?<=?239)?$i?=?$i?+?2;????????
  37. ?????????????elseif($value?>=?240?&&?$value?<=?247)?$i?=?$i?+?3;????????
  38. ?????????????else?die('Not?a?UTF-8?compatible?string');????????
  39. ?????????}????????
  40. ?????????$count++;????????
  41. ?????}????????
  42. ?????return?$count;????????
  43. }????????
  44. ??????
  45. ??????
  46. //截取字符串-UTF8(PHP)????????
  47. function?utf8_substr($str,$position,$length){????????
  48. ?????$start_position?=?strlen($str);????????
  49. ?????$start_byte?=?0;????????
  50. ?????$end_position?=?strlen($str);????????
  51. ?????$count?=?0;????????
  52. ?????for($i?=?0;?$i?strlen($str);?$i++){????????
  53. ?????????if($count?>=?$position?&&?$start_position?>?$i){????????
  54. ?????????????$start_position?=?$i;????????
  55. ?????????????$start_byte?=?$count;????????
  56. ?????????}????????
  57. ?????????if(($count-$start_byte)>=$length)?{????????
  58. ?????????????$end_position?=?$i;????????
  59. ?????????????break;????????
  60. ?????????}????????????
  61. ?????????$value?=?ord($str[$i]);????????
  62. ?????????if($value?>?127){????????
  63. ?????????????$count++;????????
  64. ?????????????if($value?>=?192?&&?$value?<=?223)?$i++;????????
  65. ?????????????elseif($value?>=?224?&&?$value?<=?239)?$i?=?$i?+?2;????????
  66. ?????????????elseif($value?>=?240?&&?$value?<=?247)?$i?=?$i?+?3;????????
  67. ?????????????else?die('Not?a?UTF-8?compatible?string');????????
  68. ?????????}????????
  69. ?????????$count++;????????
  70. ??????
  71. ?????}????????
  72. ?????return(substr($str,$start_position,$end_position-$start_position));????????
  73. }????????
  74. ??????
  75. //判断是否是有韩文-UTF-8?(JavaScript)????????
  76. function?checkKoreaChar(str)?{????????
  77. ?????for(i=0;?i<>i++)?{????????
  78. ?????????if(((str.charCodeAt(i)?>?0x3130?&&?str.charCodeAt(i)?0x318F)?||?(str.charCodeAt(i)?>=?0xAC00?&&?str.charCodeAt(i)?<=?0xD7A3)))?{????????
  79. ?????????????return?true;????????
  80. ?????????}????????
  81. ?????}????????
  82. ???? 

相关热词搜索:

上一篇:PHP 5.4.28 Released
下一篇:php5.5.x 新的特性

分享到: 收藏