Home > php教程 > php手册 > body text

php 判断是否是中文/英文/数字示例代码

WBOY
Release: 2016-06-13 12:48:01
Original
1129 people have browsed it

复制代码 代码如下:


$str='asb天水市12';

if (preg_match("/^[\x7f-\xff]+$/", $str)){
echo '全部是汉字';
}else {
echo '不全是汉字';
}
/**
PHP自带的判断是否是中文,
eregi('[^\x00-\x7F]', $str ) //中文
eregi('[0-9]', $str) //数字
eregi('[a-zA-Z]', $str)//英文
*/
if (eregi('[^\x00-\x7F]', $str) || eregi('[0-9]', $str) || eregi('[a-zA-Z]', $str)){
echo '你输入的为中英文数字的并合体哦!'.'
';
echo "长度:".strlen($str);
}
/ **

下面这两个方法是用来判断是否是英文汉字和数字组成的字符串,
或者全部是中文组成的字符串 用的变量$str还是本文开头的变量

*/

if (preg_match_all("/^([\x81-\xfe][\x40-\xfe])+$/", $str, $match)) {
echo '全部是汉字';
} else {
echo '不全是汉字';
}

if (preg_match("/([\x81-\xfe][\x40-\xfe])/", $str, $match)) {
echo '含有汉字';
} else {
echo '不含有汉字';
}

/**

此为js方法,判断了一个汉字占两个字节,一个中文或数字占一个,使用编码为UTF-8

*/

<script> <br><br>var leng = {}; <br><br>var value = document.forms[0].name.value; <BR>jmz.GetLength = function(str) { <BR>var realLength = 0, len = str.length, charCode = -1; <BR>for (var i = 0; i < len; i++) { <BR>charCode = str.charCodeAt(i); <BR>if (charCode >= 0 && charCode <= 128) realLength += 1; <BR>else realLength +=2; <BR>} <BR>return realLength; <BR>}; <br><br>alert(leng.GetLength(value)) <br><br></script>

function checkStr($str){
$output='';
$a=ereg('['.chr(0xa1).'-'.chr(0xff).']', $str);
$b=ereg('[0-9]', $str);
$c=ereg('[a-zA-Z]', $str);
if($a && $b && $c){ $output='汉字数字英文的混合字符串';}
elseif($a && $b && !$c){ $output='汉字数字的混合字符串';}
elseif($a && !$b && $c){ $output='汉字英文的混合字符串';}
elseif(!$a && $b && $c){ $output='数字英文的混合字符串';}
elseif($a && !$b && !$c){ $output='纯汉字';}
elseif(!$a && $b && !$c){ $output='纯数字';}
elseif(!$a && !$b && $c){ $output='纯英文';}
return $output;
}

echo checkStr('5爱u');

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template