php正则表达匹配中文问题分析

本文章分析了在php中正则表达式对中文的支持问题,一般情况下我们可会能简单利用我上面的正则来验证中文,后面我分析了关于兼容gb2312,utf-8和表达式 ^[x7f-xff]+$ 可以完全搞定中文不兼容的问题。

  1. $str = '中123456789abcdefg'
  2. echo preg_match("/^[u4e00-u9fa5_a-zA-Z0-9]{3,15}$",$strName); 

运行一下上面这段代码,看会有什么提示信息?

Warning: preg_match(): Compilation failed: PCRE does not support L, l, N, P, p, U, u, or X at offset 3 in F:wwwrootphptest.php on line 2

原来,PHP正则表达式中不支持下列 Perl 转义序列:L, l, N, P, p, U, u, or X,在 UTF-8 模式下,允许用“x{…}”,花括号中的内容是表示十六进制数字的字符串,原来的十六进制转义序列 xhh 如果其值大于 127 的话则匹配了一个双字节 UTF-8 字符,所以我们可以这样来解决:

  1. preg_match("/^[x80-xff_a-zA-Z0-9]{3,15}$",$strName); 
  2. preg_match('/[x{2460}-x{2468}]/u'$str); 

匹配内码汉字,按照他提供的方式进行测试,代码如下:

  1. $str = "php编程"
  2. if (preg_match("/^[x{2460}-x{2468}]+$/u",$str)) { 
  3. print("该字符串全部是中文"); 
  4. else { 
  5. print("该字符串不全部是中文"); 

发现这次依然对是否为中文判断失常,不过,既然x表示的十六进制数据,为什么和js里边提供的范围x4e00-x9fa5不一样呢?于是我就换成了下边的代码:

  1. $str = "php编程"
  2. if (preg_match("/^[x4e00-x9fa5]+$/u",$str)) { 
  3. print("该字符串全部是中文"); 
  4. else { 
  5. print("该字符串不全部是中文"); 

本来以为铁定成功了的事情,没想到,warning又一次产生了:Warning: preg_match() [function.preg-match]: Compilation failed: invalid UTF-8 string at offset 6 in test.php on line 3

看来又有错误的表达方式了,于是对照了一下那篇文章的表达方式,给“4e00”和“9fa5”两边分别用"{"和“}”包起来,跑了一遍,发现真的准确了:

  1. $str = "php编程"
  2. if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) { 
  3. print("该字符串全部是中文"); 
  4. else { 
  5. print("该字符串不全部是中文"); 

知道了php中utf-8编码下用正则表达式匹配汉字的最终正确表达式——/^[x{4e00}-x{9fa5}]+$/u,最后总结出:

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

双字节字符编码范围

1. GBK (GB2312/GB18030)

x00-xff GBK双字节编码范围

x20-x7f ASCII

xa1-xff 中文 gb2312

x80-xff 中文 gbk

2. UTF-8 (Unicode)

u4e00-u9fa5 (中文)

xAC00-xD7A3 (韩文)

u0800-u4e00 (日文)

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php正则表达匹配中文问题分析

118 评论

  1. isotretinoin 20mg sale zithromax pills ivermectin 12 mg tablets for humans

  2. order clomiphene 50mg without prescription albuterol uk no deposit casino

  3. azithromycin 500mg for sale neurontin price cheap generic neurontin

  4. real money casino app best casino order cialis 10mg pill

  5. order montelukast 10mg online brand sildenafil viagra 100mg generic

  6. online roulette for real money free casino games cheap custom essays

  7. purchase temovate sale temovate drug cordarone online buy

  8. cialis discount Cialis store natural pills for erectile dysfunction

  9. generic uroxatral diltiazem us oral diltiazem 180mg

  10. order cetirizine 10mg for sale buy zyrtec 5mg pill buy zoloft 50mg online cheap

  11. tadalafil over the counter cialis pill male erection pills

  12. order zestril 5mg pill zestril pill buy metoprolol 100mg

  13. order progesterone 200mg without prescription order zyprexa pills olanzapine cheap

  14. order indomethacin without prescription indomethacin 75mg usa buy generic suprax for sale

  15. vardenafil 10mg cost levitra for sale buy hydroxychloroquine 400mg online

  16. Piperlongumine piplartine and analogues Antiproliferative microtubule destabilising agents generic 5mg cialis best price 9 after in- vitro fertilization, suggesting that clinicians who manage infertility in these patients do not wait 12 months until attempting conception

  17. order generic diamox 250 mg buy diamox cheap cost azathioprine 25mg

  18. dapoxetine 90mg cheap cytotec pills order orlistat 120mg generic

  19. buy erythromycin 500mg without prescription erythromycin price buy nolvadex no prescription

  20. buy robaxin no prescription suhagra cost buy sildenafil generic

  21. ditropan 5mg price fosamax oral brand alendronate 35mg

  22. buy glipizide sale glipizide 10mg oral buy betamethasone without a prescription

  23. order frumil 5 mg online cheap order differin zovirax for sale

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡