Usage of the Ctype function in php

Source: Internet
Author: User
Tags alphabetic character printable characters uppercase character
This article mainly introduces the usage of the Ctype function in php, analyzes the usage of the Ctype function in detail in the form of an instance, and has some practical value for string detection, for more information about the Ctype function in php, see the following example. Share it with you for your reference. The specific analysis is as follows:

The Ctype function is Php's Ctype extension function. It provides a set of functions to verify whether the characters in the string are in the correct format, here we will mainly introduce the syntax of these string verification functions, what special functions are available, and how to verify them.

The Ctype function is a built-in PHP string body test function. It mainly includes the following types:

Ctype_alnum -- Check for alphanumeric character (s): checks 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 character control characters such as "nrt ".

Ctype_digit -- Check for numeric character (s): Check whether a 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 all characters are in lowercase.

Ctype_print -- Check for printable character (s): checks whether a 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 it is a hexadecimal string and can only contain "0123456789abcdef"

How to use them, directly go to the code:

The code is as follows:

$ Str1 = 'azxc1234'; // true
$ Str2 = '2014 # taobao'; // false
If (ctype_alnum ($ string )){
Echo "This string totally works ";
}
Else {
Echo "And this one not so much ";
}


Note: If the first empty string is passed, false is returned after php5.1, but true is returned in earlier versions.

The code is as follows:

The code is as follows:

$ Integer = 42;
Ctype_digit ($ integer); // false
Ctype_digit (string) $ integer); // true


Conclusion: the same operation may have different methods when using PHP. it is important to select an appropriate method. In PHP, there are two types of string-type functions: ctype _ * and is _ *. in actual applications, they use the is _ * series of functions, of course, in addition to these methods, we can also use regular expressions for judgment, which is more powerful, but using PHP built-in functions can better reduce the error rate.

I hope this article will help you with PHP programming.

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.