Phpctype function translation and examples in Chinese _ PHP Tutorial

Source: Internet
Author: User
Tags alphabetic character printable characters uppercase character
Phpctype function translation and examples. PHPCtype extensions are built-in extensions at the beginning of PHP4.2. Note that all Ctype functions have only one string type parameter and return a Boolean value. Copy the code as follows: $ str0.11 PHP Ctype extension is a built-in extension starting with PHP4.2. Note that all the Ctype functions have only one string type parameter, and they return a Boolean value.

The code is as follows:


$ Str = "0.1123 ";
// Check whether all characters in the string are numbers
Echo "ctype_digit:". ctype_digit ($ str); // null
// Check whether it is a numeric string, which can be a negative number or a decimal number
Echo "is_numberic:". is_numeric ($ str); // 1

We can see the difference between ctype_digit () and is_numberic.

Chinese translation

The Ctype function is a built-in PHP string body test function. There are mainly the following types

Ctype_alnum -- Check for alphanumeric character (s)
Check if it only contains [A-Za-z0-9]

Ctype_alpha -- Check for alphabetic character (s)
Check whether it only contains [A-Za-z]

Ctype_cntrl -- Check for control character (s)
Check whether it only contains character control characters such as "\ n \ r \ t ".

Ctype_digit -- Check for numeric character (s)
Check that the string contains only numbers (0-9)

Ctype_graph -- Check for any printable character (s) character t space
Check whether the string contains only printable characters (except spaces)

Ctype_lower -- Check for lowercase character (s)
Check whether all characters are English letters and lowercase letters.

Ctype_print -- Check for printable character (s)
Check whether the string contains only printable characters.

Ctype_punct -- Check for any printable character which is not whitespace or an alphanumeric character
Check whether only printable characters including non-numbers, characters, and spaces are allowed.

Ctype_space -- Check for whitespace character (s)
Check whether it only contains characters and spaces such "".

Ctype_upper -- Check for uppercase character (s)
Check whether all characters are English letters and uppercase letters

Ctype_xdigit -- Check for character (s) representing a hexadecimal digit
Check whether the string is a hexadecimal string and can only contain "0123456789abcdef"

Some examples are provided.

We usually do not want to write regular expressions when performing simple filtering on some forms. in terms of efficiency, regular expressions are also one of the reasons that affect the PHP running speed, therefore, try not to use regular expressions when you try not to use regular expressions. Fortunately, PHP has taken this into consideration and provided me with the Ctype function. Next, we will give a brief introduction to some Ctype functions to use them as an alternative:
1. ctype_alnum-Check for alphanumeric character (s) Check string contains only numbers or letters, equivalent to regular [A-Za-z0-9]. there is a return value. Returns TRUE if the call is successful, and FALSE if the call fails;
[

The code is as follows:


$ Strings = array ('abc1zyz9', 'foo! # $ Bar ');
Foreach ($ strings as $ testcase ){
If (ctype_alnum ($ testcase )){
Echo "The string $ testcase consists of all letters or digits. \ n"; \ output The string AbCd1zyZ9 consists of all letters or digits.
} Else {
Echo "The string $ testcase does not consist of all letters or digits. \ n" ;\\ output The string foo! # $ Bar does not consist of all letters or digits.
}
}
?>

2. the ctype_alpha-Check for alphabetic character (s) Check string contains only letters. Returns TRUE if the call is successful, and FALSE if the call fails;

The code is as follows:


$ Strings = array ('kjgwzc', 'arf12 ');
Foreach ($ strings as $ testcase ){
If (ctype_alpha ($ testcase )){
Echo "The string $ testcase consists of all letters. \ n" ;\\ output The string KjgWZC consists of all letters.
} Else {
Echo "The string $ testcase does not consist of all letters. \ n"; \ output The string arf12 does not consist of all letters.
}
}
?>

3. ctype_cntrl-Check for control character (s) Check whether the string contains only control characters such as '\ n' \ R'' \ t.

The code is as follows:


$ Strings = array ('string1' => "\ n \ r \ t", 'string2' => 'arf12 ');
Foreach ($ strings as $ name => $ testcase ){
If (ctype_cntrl ($ testcase )){
Echo "The string '$ name' consists of all control characters. \ n"; \ output The string 'string1' consists of all control characters.
} Else {
Echo "The string '$ name' does not consist of all control characters. \ n"; \ The string 'string2' does not consist of all control characters.
}
}
?>

4. ctype_digit-Check for numeric character (s) Check whether the string contains only numbers

The code is as follows:


$ Strings = array ('1970. 20', '123', 'wsl! 12 ');
Foreach ($ strings as $ testcase ){
If (ctype_digit ($ testcase )){
Echo "The string $ testcase consists of all digits. \ n ";
} Else {
Echo "The string $ testcase does not consist of all digits. \ n ";
}
}
?>

Http://www.bkjia.com/PHPjc/744329.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/744329.htmlTechArticlePHP Ctype extensions are built-in extensions at the beginning of PHP4.2. Note that the Ctype series functions all have only one string type parameter and they return a Boolean value. The code is as follows: $ str = "0. 11...

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.