PHP | ctype_alnum() (Check for Alphanumeric)

A ctype_alnum() function in PHP used to check all characters of given string/text are alphanumeric or not. If all characters are alphanumeric then return TRUE, otherwise return FALSE.

Syntax:

 bool ctype_alnum ($text)

Parameters Used:



  • $text : It is a mandatory parameter which specifies the string.

Examples:

Input  : Geeks
Output : Yes
Explanation : String "Geeks" is alphanumeric.
              Note: Read Standard c local letter.
      
Input  : '%%%Contribute_article on GFG!!!'
Output : No
         
Explanation : String contains Special characters.
                 
Note: Except string or digit, if we input anything then it will return FALSE.

Below programs illustrate the ctype_alnum() function.

Program :1
Drive a code ctype_alnum()() function for better understand.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// PHP program to check given string is 
// all characters are alphanumeric
  
$string = 'GeeksforGeeks';
  
    if ( ctype_alnum($string))
        echo "Yes\n";
    else 
        echo "No\n";
  
?>

chevron_right


Output:

Yes

Program: 2 Drive a code of ctype_alnum() function where input will be a array of string integers, string with special symbols.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check given string is 
// all characters are alphanumeric
  
$strings = array(
    'Geeks',
    'geek@gmail.com',
    '2018',
    'GFG2018',
    'a b c ',
    '@!$%^&*()|2018'
);
  
// Checking above given four strings 
// by used of  ctype_alnum() function .
foreach ($strings as $test) {
      
    if (ctype_alnum($test))
        echo "Yes\n";
    else
        echo "No\n";
      
}
  
?>

chevron_right


Output:

Yes
No
Yes
Yes
No
No

References :http://php.net/manual/en/function.ctype-alnum.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.