首页 > 技术文章 > ^[A-Za-z0-9\u4E00-\u9FA5-]{2,16}$ 对英文、数字、中文的验证

feng12345 2016-05-04 23:51 原文

^[A-Za-z0-9\u4E00-\u9FA5-]{2,16}$

\u4e00-\u9fa5; 

 

php正则表达式匹配汉字:

根据页面编码: 

1.gb231

<?php
$str="i love 你 my 祖国!";
preg_match_all("/[\x80-\xff]+/",$str,$match);
print_r($match);
?>

 

 2.utf-8

<?php
$str="zhong中国guo我爱你";
preg_match_all("/[\x{4E00}-\x{9FA5}]+/u",$str,$match);(模式修饰符u代表模式字符串是utf-8模式)
print_r($match);
?>

文章链接 

 引用:

 

正则匹配中文汉字

正则匹配中文汉字根据页面编码不同而略有区别:

  • GBK/GB2312编码:[x80-xff]+ 或 [xa1-xff]+
  • UTF-8编码:[x{4e00}-x{9fa5}]+/u

例子:

<?php $str = "学习php是一件快乐的事。"; preg_match_all("/[x80-xff]+/", $str, $match); //UTF-8 使用: //preg_match_all("/[x{4e00}-x{9fa5}]+/u", $str, $match); print_r($match); ?> 

输出:

Array (     [0] => Array         (             [0] => 学习             [1] => 是一件快乐的事。         )   )

 

推荐阅读