Home Backend Development PHP Tutorial php 判断是否是中文/英文/数字示例代码_php技巧

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

May 17, 2016 am 08:54 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');
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 Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles