PHP to determine whether the Chinese/English/Digital sample code _php Tips

Source: Internet
Author: User
Copy Code code as follows:

$str = ' ASB Tianshui 12 ';

if (Preg_match ("/^[\x7f-\xff]+$/", $str)) {
Echo ' All is Chinese character ';
}else {
Echo ' is not all Chinese characters ';
}
/**
PHP's own judgment 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 ' you entered the combination of Chinese and English numerals! ' <br> ';
echo "Length:". strlen ($STR);
}
/ **

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

*/

if (Preg_match_all ("/^ ([\x81-\xfe][\x40-\xfe]) +$/", $str, $match)) {
Echo ' All is Chinese character ';
} else {
Echo ' is not all Chinese characters ';
}

if (Preg_match ("/([\x81-\xfe][\x40-\xfe])/", $str, $match)) {
Echo ' contains Chinese characters ';
} else {
Echo ' does not contain Chinese characters ';
}

/**

This is the JS method, judged a Chinese character for two bytes, one Chinese or the number of one, using encoding for UTF-8

*/

<script>

var leng = {};

var value = Document.forms[0].name.value;
Jmz. GetLength = function (str) {
var reallength = 0, Len = str.length, charcode =-1;
for (var i = 0; i < len; i++) {
CharCode = Str.charcodeat (i);
if (charcode >= 0 && charcode <= 128) Reallength + = 1;
else Reallength +=2;
}
return reallength;
};

Alert (Leng. GetLength (value))

</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 = ' mixed string ' in Chinese character digital English;}
ElseIf ($a && $b &&! $c) {$output = ' mixed string of kanji numbers ';}
ElseIf ($a &&! $b && $c) {$output = ' mixed string ' in English characters ';}
ElseIf (! $a && $b && $c) {$output = ' mixed string in digital English ';}
ElseIf ($a && $b &&! $c) {$output = ' pure Chinese ';}
ElseIf (! $a && $b &&! $c) {$output = ' pure number ';}
ElseIf (! $a && $b && $c) {$output = ' plain English ';}
return $output;
}

Echo Checkstr (' 5 love u ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.