![微笑](http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif) 在javascript中,要判断字符串是中文是很简单的。比如: var str = "php编程";
if (/^[\u4e00-\u9fa5]+$/.test(str)) {
<span style="font-family: Arial, Helvetica, sans-serif;">alert("该字符串全部是中文");</span>
} else {
alert("该字符串不全部是中文");
} 在php中来判断字符串是否为中文: $str = "php编程";
if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}
---------------------------------------------------------------------------------------------------------------------------------- 参考以上文章写了如下一段测试代码(复制以下代码保存成.php文件) <?php
$action = trim($_GET['action']);
if($action == "sub"){
<span style="white-space:pre"> </span>$str = $_POST['dir'];
<span style="white-space:pre"> </span>//if(!preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str)) //GB2312汉字字母数字下划线正则表达式
<span style="white-space:pre"> </span>if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$str)) //UTF-8汉字字母数字下划线正则表达式
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>echo"<font color=red>您输入的[".$str."]含有违法字符</font>";
<span style="white-space:pre"> </span>}else{
<span style="white-space:pre"> </span>echo "<font color=green>您输入的[".$str."]完全合法,通过!</font>";
<span style="white-space:pre"> </span>}
}
?>
<form method="POST" action="?action=sub">
<span style="white-space:pre"> </span>输入字符(数字,字母,汉字,下划线):
<span style="white-space:pre"> </span><input type="text" name="dir" value="">
<span style="white-space:pre"> </span><input type="submit" value="提交">
</form>
(转) |