This article mainly introduces Chinese translation and examples of phpctype functions. there are multiple examples of related functions. if you need them, refer to the PHP Ctype extension, which is a built-in extension starting with PHP4.2. note, all Ctype functions have only one string type parameter and 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 ";
}
}
?>