The CType function usage in PHP is detailed

Source: Internet
Author: User
Tags alphabetic character numeric lowercase types of functions uppercase character

This article analyzes the usage of CType function in PHP. Share to everyone for your reference. The specific analysis is as follows:

The CType function is a php CType extension function that provides a set of functions to verify that the characters in the string are in the correct format, here we introduce the syntax of these string validation functions, what special functions they have, how to verify them, and so on.

The CType function is a built-in string-testing function of PHP, which has the following main types:

Ctype_alnum-Check for alphanumeric character (s): Detect if it contains only [a-za-z0-9]

Ctype_alpha-Check for alphabetic character (s): Detect if it contains only [a-za-z]

Ctype_cntrl--Check for control character (s): Check to see if it is a character-controlled character that contains only the class "NRT"

Ctype_digit-Check for numeric character (s): Check is a string containing only numeric characters (0-9)

Ctype_graph--Check for any printable character (s) except spaces: Check for strings that contain only characters that can be printed (except spaces)

Ctype_lower-Check for lowercase character (s): Check that all characters are in English letters and are lowercase

Ctype_print-Check for printable character (s): Check if it is a string containing only characters that can be printed

CTYPE_PUNCT--Check for any printable character which are not whitespace or a alphanumeric character: check to see if it contains only non-numeric/character/space Print out the characters

Ctype_space-Check for whitespace character (s): Check if it is a character and space that contains only the class as ""

Ctype_upper-Check for uppercase character (s): Check that all characters are in English letters and are all uppercase

Ctype_xdigit-Check for character (s) representing a hexadecimal digit: check if it is a 16-in string and can only include "0123456789abcdef"

How to use them, directly on the code:

The code is as follows:

$str 1 = ' Azxc1234 ';//true

$str 2 = ' 123#axy ';//false

if (Ctype_alnum ($string)) {

echo "This string totally works";

}

else {

echo "And this one isn't so much";

}

Note: If you pass the first empty string, it returns false after php5.1, but the earlier version returns True.

As long as you ensure that the argument passed to the function is a string there is no problem with the code 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, the same operation may have different methods, it is obviously important to choose a suitable method. There are two types of functions that judge string types in PHP, ctype_* and is_* they is_* this series of functions in a practical application, except that we can choose to use regular expressions to make judgments and to be more powerful, But using PHP's built-in functions can be a better way to reduce the error rate.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.