Friday, 19 September 2014

PHP: ctype Functions in PHP

ctype_alpha — Check for alphabetic character(s)

Description:
bool ctype_alpha ( string $text )
Checks if all of the characters in the provided string, text, are alphabetic.
In the standard C locale letters are just [A-Za-z] and ctype_alpha() is equivalent to (ctype_upper($text) || ctype_lower($text)) if $text is just a single character, but other languages have letters that are considered neither upper nor lower case.

Parameters:
text:The tested string.
Return Values:Returns TRUE if every character in text is a letter from the current locale, FALSE otherwise.

Example #1 A ctype_alpha() example (using the default locale)
<?php
$strings = array('KjgWZC', 'arf12');
foreach ($strings as $testcase) {
    if (ctype_alpha($testcase)) {
        echo "The string $testcase consists of all letters.\n";
    } else {
        echo "The string $testcase does not consist of all letters.\n";
    }
}
?>
Output:

The string KjgWZC consists of all letters.
The string arf12 does not consist of all letters.

-----------------------------------------------------------------------------------
ctype_alnum — Check for alphanumeric character(s).

Description
bool ctype_alnum ( string $text )

Checks if all of the characters in the provided string, text, are alphanumeric.
Parameters:
text:The tested string.
Return Values: Returns TRUE if every character in text is either a letter or a digit, FALSE otherwise.

Example #1 A ctype_alnum() example (using the default locale)

<?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";
    } else {
        echo "The string $testcase does not consist of all letters or digits.\n";
    }
}
?>

Output:

The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.
----------------------------------------------------------------------------------------------------
ctype_cntrl — Check for control character(s)

Description:
bool ctype_cntrl ( string $text )
Checks if all of the characters in the provided string, text, are control characters. Control characters are e.g. line feed, tab, escape.

Parameters:
text:The tested string.
Return Values:Returns TRUE if every character in text is a control character from the current locale, FALSE otherwise.
Example #1 A ctype_cntrl() example

<?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";
    } else {
        echo "The string '$name' does not consist of all control characters.\n";
    }
}
?>
Output:
The string 'string1' consists of all control characters.
The string 'string2' does not consist of all control characters.
--------------------------------------------------------------------------------------------------------------
ctype_digit — Check for numeric character(s)

Description:
bool ctype_digit ( string $text )
Checks if all of the characters in the provided string, text, are numerical.

Parameters:
text:The tested string.
Return Values :Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.

Version    Description
5.1.0    Before PHP 5.1.0, this function returned TRUE when text was an empty string.

Example #1
<?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 of all digits.\n";
    }
}
?>
Output:

The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.
Example #2 A ctype_digit() example comparing strings with integers

<?php

$numeric_string = '42';
$integer        = 42;

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

is_numeric($numeric_string);   // true
is_numeric($integer);          // true
?>
--------------------------------------------------------------------------------------------------------------
ctype_graph — Check for any printable character(s) except space
Description:
bool ctype_graph ( string $text )
Checks if all of the characters in the provided string, text, creates visible output.

Parameters:
text:The tested string.
Return Values:Returns TRUE if every character in text is printable and actually creates visible output (no white space), FALSE otherwise.

Example #1:
<?php
$strings = array('string1' => "asdf\n\r\t", 'string2' => 'arf12', 'string3' => 'LKA#@%.54');
foreach ($strings as $name => $testcase) {
    if (ctype_graph($testcase)) {
        echo "The string '$name' consists of all (visibly) printable characters.\n";
    } else {
        echo "The string '$name' does not consist of all (visibly) printable characters.\n";
    }
}
?>
Output:

The string 'string1' does not consist of all (visibly) printable characters.
The string 'string2' consists of all (visibly) printable characters.
The string 'string3' consists of all (visibly) printable characters.
---------------------------------------------------------------------------------------------
ctype_lower — Check for lowercase character(s)

Description
bool ctype_lower ( string $text )
Checks if all of the characters in the provided string, text, are lowercase letters.

Parameters:
text:The tested string.

Return Values:Returns TRUE if every character in text is a lowercase letter in the current locale.

Example #1 A ctype_lower() example (using the default locale)

<?php
$strings = array('aac123', 'qiutoas', 'QASsdks');
foreach ($strings as $testcase) {
    if (ctype_lower($testcase)) {
        echo "The string $testcase consists of all lowercase letters.\n";
    } else {
        echo "The string $testcase does not consist of all lowercase letters.\n";
    }
}
?>
Output:

The string aac123 does not consist of all lowercase letters.
The string qiutoas consists of all lowercase letters.
The string QASsdks does not consist of all lowercase letters.
-------------------------------------------------------------------------------------
ctype_print — Check for printable character(s)

Description:
bool ctype_print ( string $text )
Checks if all of the characters in the provided string, text, are printable.

Parameters:
text:The tested string.
Return Values:Returns TRUE if every character in text will actually create output (including blanks). Returns FALSE if text contains control characters or characters that do not have any output or control function at all.

Example #1 A ctype_print() example

<?php
$strings = array('string1' => "asdf\n\r\t", 'string2' => 'arf12', 'string3' => 'LKA#@%.54');
foreach ($strings as $name => $testcase) {
    if (ctype_print($testcase)) {
        echo "The string '$name' consists of all printable characters.\n";
    } else {
        echo "The string '$name' does not consist of all printable characters.\n";
    }
}
?>
Output:

The string 'string1' does not consist of all printable characters.
The string 'string2' consists of all printable characters.
The string 'string3' consists of all printable characters.
-------------------------------------------------------------------------------------------
ctype_punct — Check for any printable character which is not whitespace or an alphanumeric character

Description:
bool ctype_punct ( string $text )
Checks if all of the characters in the provided string, text, are punctuation character.

Parameters:
text:The tested string.
Return Values :Returns TRUE if every character in text is printable, but neither letter, digit or blank, FALSE otherwise.

Example #1 A ctype_punct() example

<?php
$strings = array('ABasdk!@!$#', '!@ # $', '*&$()');
foreach ($strings as $testcase) {
    if (ctype_punct($testcase)) {
        echo "The string $testcase consists of all punctuation.\n";
    } else {
        echo "The string $testcase does not consist of all punctuation.\n";
    }
}
?>
Output:

The string ABasdk!@!$# does not consist of all punctuation.
The string !@ # $ does not consist of all punctuation.
The string *&$() consists of all punctuation.
------------------------------------------------------------------------------------------------
ctype_space — Check for whitespace character(s)

Description:
bool ctype_space ( string $text )
Checks if all of the characters in the provided string, text, creates whitespace.

Parameters:
text:The tested string.
Return Values :Returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.

Example #1 A ctype_space() example

<?php
$strings = array('string1' => "\n\r\t", 'string2' => "\narf12", 'string3' => '\n\r\t');
foreach ($strings as $name => $testcase) {
    if (ctype_space($testcase)) {
        echo "The string '$name' consists of all whitespace characters.\n";
    } else {
        echo "The string '$name' does not consist of all whitespace characters.\n";
    }
}
?>
Output:

The string 'string1' consists of all whitespace characters.
The string 'string2' does not consist of all whitespace characters.
The string 'string3' does not consist of all whitespace characters.
---------------------------------------------------------------------------------------------------
ctype_upper — Check for uppercase character(s)

Description:
bool ctype_upper ( string $text )
Checks if all of the characters in the provided string, text, are uppercase characters.

Parameters:
text:The tested string.
Return Values:Returns TRUE if every character in text is an uppercase letter in the current locale.

Example #1 A ctype_upper() example (using the default locale)

<?php
$strings = array('AKLWC139', 'LMNSDO', 'akwSKWsm');
foreach ($strings as $testcase) {
    if (ctype_upper($testcase)) {
        echo "The string $testcase consists of all uppercase letters.\n";
    } else {
        echo "The string $testcase does not consist of all uppercase letters.\n";
    }
}
?>
Output:

The string AKLWC139 does not consist of all uppercase letters.
The string LMNSDO consists of all uppercase letters.
The string akwSKWsm does not consist of all uppercase letters.
-----------------------------------------------------------------------------------------------------
ctype_xdigit — Check for character(s) representing a hexadecimal digit

Description:
bool ctype_xdigit ( string $text )
Checks if all of the characters in the provided string, text, are hexadecimal 'digits'.

Parameters:
text:The tested string.

Return Values:Returns TRUE if every character in text is a hexadecimal 'digit', that is a decimal digit or a character from [A-Fa-f] , FALSE otherwise.

Example #1 A ctype_xdigit() example

<?php
$strings = array('AB10BC99', 'AR1012', 'ab12bc99');
foreach ($strings as $testcase) {
    if (ctype_xdigit($testcase)) {
        echo "The string $testcase consists of all hexadecimal digits.\n";
    } else {
        echo "The string $testcase does not consist of all hexadecimal digits.\n";
    }
}
?>
Output:

The string AB10BC99 consists of all hexadecimal digits.
The string AR1012 does not consist of all hexadecimal digits.
The string ab12bc99 consists of all hexadecimal digits.

0 comments:

Post a Comment