In some scenarios, such as login, we restrict the user name to a combination of numbers and letters. Normally, we would use regular expressions to do this, but PHP has already provided several functions to handle this.

Ctype_ Correlation functions

// Numbers and letters, not including floating point numbers, [A-za-z0-9]
if(ctype_alnum($input)) {echo $input.'is a combination of English digits! ', PHP_EOL;
}

// English letters, [a-za-z]
if(ctype_alpha($input)) {echo $input.'is an English letter! ', PHP_EOL;
}

// Numeric characters, excluding floating point numbers, negative numbers (unsigned positive integers)
if(ctype_digit($input)) {echo $input.'is a numeric character! ', PHP_EOL;
}
Copy the code

The above three functions are divided into three character types of numbers + letters, pure letters and pure numbers respectively. Yes, it is the functions starting with ctype_ that we use for this determination. The only thing to note here is that ctype_digit() returns true only if it is an unsigned, positive integer without a decimal point, meaning that negative numbers and decimals are not allowed to pass this function either. We’ll also look at the difference between ctype_digit() and is_numeric() at the end of the article.

Of course, there are more than three ctype_ related functions, but let’s look at another set.

// Lowercase letters
if(ctype_lower($input)) {echo $input.'is a lowercase character! ', PHP_EOL;
}

// Uppercase letters
if(ctype_upper($input)) {echo $input.'is an uppercase character! ', PHP_EOL;
}

// All printable characters
if(ctype_print($input)) {echo $input.'is a printable character! ', PHP_EOL;
}

// All characters are visible, except for Spaces or formatting controls that are not
if(ctype_graph($input)) {echo $input.'is printable character, except whitespace character! ', PHP_EOL;
}

// A printable character punctuation class that does not contain whitespace, letters, or digits
if(ctype_punct($input)) {echo $input.'is a printable character that does not contain whitespace, letters, or digits! ', PHP_EOL;
}
Copy the code

Ctype_lower () and ctype_upper() determine whether the contents of the string are all lowercase or uppercase. Remember, everything in the string must be either uppercase or lowercase, and you can’t return true if one letter isn’t. Of course, these two judgments must also contain English letters, no numbers, Spaces and so on.

Ctype_print () checks if it is printable. What is printable content? It’s something like echo, print, something that you can print and you can see it on the screen.

Ctype_graph () is a printable character other than whitespace. What is a whitespace character? Tabs such as \n and \t, as well as our normal Spaces, are whitespace characters.

Ctype_punct () can be simply interpreted as a punctuation symbol. The entire string is composed of punctuation symbols and does not contain blank content.

// \n \t \r and so on
if(ctype_cntrl($input)) {echo $input.'is a format control character! ', PHP_EOL;
}

/ / space
if(ctype_space($input)) {echo $input.'is a space character! ', PHP_EOL;
}

// Hexadecimal AB10BC99
if(ctype_xdigit($input)) {echo $input.'is a hexadecimal character! ', PHP_EOL;
}
Copy the code

Ctype_cntrl () determines whether the entire character is made up of tabs.

Ctype_space () is a string consisting of Spaces, as the name indicates.

Ctype_xdigit () is our last ctype_ function, which, as its name implies, checks whether it is a hexadecimal string.

Difference between ctype_digit() and is_numeric()

Both of these functions determine whether an argument is a number, but one big difference is that ctype_digit() applies only to strings. That is, it is used to determine whether the content of a string is a number. If you give it a normal number type, it will still return false. It will assume that the content of the number type is not a string number.

// the difference between ctype_digit and is_numeric
$numeric_string = The '42';
$integer        = 42;

echo ctype_digit($numeric_string), PHP_EOL;  // true
echo ctype_digit($integer), PHP_EOL;         // false (ASCII 42 is the * character)

echo is_numeric($numeric_string), PHP_EOL;   // true
echo is_numeric($integer), PHP_EOL;          // true
Copy the code

conclusion

Is not a convenient set of functions. However, it is also very convenient to use the re now, and the re combinations are more diverse, we can control more, so re is still the first choice in this case. Of course, the more flexible you are, the more likely you are to make mistakes, and missing a bit of regex without exhaustive testing can have serious consequences. Therefore, in daily work, the specific choice or to see our own, here is just for everyone to make a popular science introduction, the use of what kind of decision is still in our hands.

It should also be noted that this set of ctype_ functions does not support Chinese, they only support English, numbers, English symbols, such as the judgment.

Test code: github.com/zhangyue050…

Reference documents: www.php.net/manual/zh/r…