Monday, 26 August 2013

PHP Function ctype_alnum()

Syntax

ctype_alnum ( $text );

Definition and Usage

Checks if all of the characters in the provided string, text, are alphanumeric. In the standard C locale letters are just [A-Za-z] and the function is equivalent to preg_match('/^[a-z0-9]+$/iD', $text).

Paramters

ParameterDescription
textRequired. The tested string.

Return Value

Returns TRUE if every character in text is either a letter or a digit, FALSE otherwise.

Example

Try out following example:
<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
 if (ctype_alnum($testcase)) {
     echo "$testcase consists of all letters or digits.<br />";
 } else {
     echo "$testcase does not have all letters or digits.<br />";
 }
}
?> 
This will produce following result:
AbCd1zyZ9 consists of all letters or digits.
foo!#$bar does not have all letters or digits.

No comments:

Post a Comment