PHP determines whether it is Chinese/English/numeric sample code_PHP tutorial

WBOY
Release: 2016-07-21 16:13:11
Original
997 people have browsed it

Copy code The code is as follows:

$str='asbTianshui City 12';

if (preg_match(" /^[x7f-xff]+$/", $str)){
echo 'All are Chinese characters';
}else {
echo 'Not all are Chinese characters';
}
/**
PHP comes with the ability to determine whether it is Chinese,
eregi('[^x00-x7F]', $str ) //Chinese
eregi('[0-9]', $str) // Number
eregi('[a-zA-Z]', $str)//English
*/
if (eregi('[^x00-x7F]', $str) || eregi('[0-9]', $str) || eregi('[a- zA-Z]', $str)){
echo 'What you entered is a combination of Chinese and English numbers!'.'
';
echo "Length:".strlen($str );
}
/ **

The following two methods are used to determine whether it is a string composed of English characters and numbers,
or a string composed entirely of Chinese characters. The variable $str is still the variable at the beginning of this article

*/

if (preg_match_all("/^([x81-xfe][x40-xfe])+$/", $str, $match)) {
echo 'All are Chinese characters';
} else {
echo 'Not all are Chinese characters';
}

if (preg_match("/([x81 -xfe][x40-xfe])/", $str, $match)) {
echo 'Contains Chinese characters';
} else {
echo 'Does not contain Chinese characters';
}

/**

This is a js method. It determines that a Chinese character occupies two bytes, and a Chinese character or number occupies one. The encoding is 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='A mixed string of Chinese characters, numbers and English';}
elseif($a && $b && !$c){ $output='A mixed string of Chinese characters and numbers';}
elseif($ a && !$b && $c){ $output='A mixed string of Chinese characters and English';}
elseif(!$a && $b && $c){ $output='A mixed string of numbers and English' ;}
elseif($a && !$b && !$c){ $output='Pure Chinese characters';}
elseif(!$a && $b && !$c){ $output='Pure Number';}
elseif(!$a && !$b && $c){ $output='Pure English';}
return $output;
}

echo checkStr( '5爱u');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313518.htmlTechArticleCopy the code as follows: $str='asbTianshui City 12'; if (preg_match("/^[x7f -xff]+$/", $str)){ echo 'All are Chinese characters'; }else { echo 'Not all are Chinese characters'; } /** PHP's built-in method of judging whether it is...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template