zoukankan      html  css  js  c++  java
  • ^[A-Za-z0-9u4E00-u9FA5-]{2,16}$ 对英文、数字、中文的验证

    ^[A-Za-z0-9u4E00-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] => 是一件快乐的事。         )   )

  • 相关阅读:
    PHP base64
    JS 获取url参数
    PHP 微信分享
    symfony安装笔记
    php 中文繁简体转换
    php webservice
    win10+PHP7
    JS弹出浮层
    CentOS7 Nginx负载均衡
    inotify+rsync目录实时同步
  • 原文地址:https://www.cnblogs.com/feng12345/p/5460289.html
Copyright © 2011-2022 走看看