现在的位置: 首页 > 综合 > 正文

php汉字匹配正则

2012年08月14日 ⁄ 综合 ⁄ 共 468字 ⁄ 字号 评论关闭
一般编码有两种
UTF8和GBK,所以匹配也要分两种(看你站点编码设置了)
 

代码

<?php
$action = trim($_GET['action']);
if($action == "sub")
{
    
$str = $_POST['dir'];   
    
//if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str)) //GB2312汉字字母数字下划线正则表达式
    if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$str))   //UTF-8汉字字母数字下划线正则表达式
    {  
        
echo "<font color=red>您输入的[".$str."]含有违法字符</font>";  
    }
    
else
    {
        
echo "<font color=green>您输入的[".$str."]完全合法,通过!</font>";  
    }
}
?>

抱歉!评论已关闭.