php 判断是否是中文/英文/数字示例代码_PHP
Jun 01, 2016 am 11:59 AM
php
English
复制代码 代码如下:
$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');
$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');
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

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
