PHP CType Function Chinese Translation and example _php instance

Source: Internet
Author: User
Tags alphabetic character control characters lowercase printable characters uppercase character

PHP CType Extensions are extensions that PHP4.2 started, and note that CType series functions have only one string type parameter that returns a Boolean value.

Copy Code code as follows:

$str = "0.1123";
Check whether all characters of a string are numbers
echo "Ctype_digit:".  Ctype_digit ($STR); Empty
Detects if a numeric string, can be negative and decimal
echo "Is_numberic:". Is_numeric ($STR); 1

From above you can see the difference between ctype_digit () and Is_numberic ().

Chinese translation

The CType function is a string-body-testing function built into PHP. Mainly has the following several

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 whether it is a character control character that contains only the class is "\n\r\t"

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

Ctype_graph--Check for any printable character (s) except spaces
Check whether it is a string that contains only characters that can be printed (except spaces)

Ctype_lower--Check for lowercase character (s)
Check to see if all the characters are in English letters and are all lowercase

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

CTYPE_PUNCT--Check for any printable character which are not whitespace or a alphanumeric
Check for printable characters that contain only non-numeric/character/space

Ctype_space--Check for whitespace character (s)
Check for characters and spaces that contain 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"

There are examples of yo

We usually encounter to some form to do a simple filter, often not too willing to write regular, and in efficiency, is also affecting the speed of PHP is one of the reasons, so can not try the regular time as far as possible without trial. Luckily, PHP has given this to us and provided me with the CType function. The following is a brief introduction to some of the CType functions to spare:
1. Ctype_alnum-check for alphanumeric character (s) checks that the string contains only numbers or letters, equivalent to regular [a-za-z0-9]. There is a return value. Returns true on success and fails to false;
[

Copy Code code as follows:

<?php
$strings = Array (' Abcd1zyz9 ', ' 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 to all letters or digits.\n"; \ \ Output The string foo!# $bar does not consist to all letters or digits.
}
}
?>

2, Ctype_alpha-check for alphabetic character (s) check that the string contains only letters. Returns true on success and fails to false;

Copy Code code as follows:

<?php
$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 to all letters.\n"; <span style= "White-space:pre" > </span>\\ output T He string ARF12 does not consist the all letters.
}
}
?>

3, Ctype_cntrl-check for control character (s) checks whether the string contains only "\ n" ' \ R ' s "" \ \ T "" ".

Copy Code code as follows:

<?php
$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 to all control characters.\n"; \ \ The string ' string2 ' does not consist to all control characters.
}
}
?>

4, Ctype_digit-check for numeric character (s) check whether the string contains only numbers

Copy Code code as follows:

<?php
$strings = Array (' 1820.20 ', ' 10002 ', ' 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 to all digits.\n";
}
}
?>

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.