Home >Backend Development >PHP Tutorial >Detailed explanation of Ctype function usage in php_PHP tutorial
This article analyzes the usage of Ctype function in php with examples. Share it with everyone for your reference. The specific analysis is as follows:
The Ctype function is the Ctype extension function of Php. It provides a set of functions for verifying whether the characters in the string are in the correct format. Here we mainly introduce the syntax of these string verification functions and what special functions there are. How to verify etc.
The Ctype function is PHP’s built-in string body testing function. There are mainly the following types:
ctype_alnum -- Check for alphanumeric character(s): Check whether 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 control characters whose class is "nrt" and so on
ctype_digit -- Check for numeric character(s): When checking, it is a string containing only numeric characters (0-9)
ctype_graph -- Check for any printable character(s) except space: Check whether it is a string containing only printable characters (except spaces)
ctype_lower -- Check for lowercase character(s): Check whether all characters are English letters and all are lowercase
ctype_print -- Check for printable character(s): Check whether it is a string containing only printable characters
ctype_punct -- Check for any printable character which is not whitespace or an alphanumeric character: Check whether it is a printable character that only contains non-digits/characters/spaces
ctype_space -- Check for whitespace character(s): Check whether it only contains characters and spaces such as " "
ctype_upper -- Check for uppercase character(s): Check whether all characters are English letters and all are uppercasectype_xdigit -- Check for character(s) representing a hexadecimal digit: Check whether it is a hexadecimal string, which can only include "0123456789abcdef"
How to use them, just go to the code:
The code is as follows:
$str1 = 'Azxc1234';//true
$str2 = '123#Axy';//false
if (ctype_alnum($string)) {
echo "This string totally works";
}
else {
echo "And this one not so much";
}
Note: If you pass the first empty string, false will be returned after php5.1, but earlier versions will return true.
As long as you make sure the parameter passed to the function is a string, there will be no problem. The code is as follows:
The code is as follows:
$integer = 42;
ctype_digit($integer); // false
ctype_digit((string)$integer); // true
Conclusion: In the process of using PHP, there may be different methods for the same operation, and it is obviously important to choose an appropriate method. There are two types of functions to determine the string type in PHP, ctype_* and is_*. In actual applications, the is_* series of functions are more commonly used. Of course, in addition to these methods, we can also choose to use regular expressions. Make judgments and is more powerful, but using PHP's built-in functions can better reduce the error rate.