PHP Tutorial : Ctype character type checking

Happy 1st June for those under 18

Yesterday I’ve found a nice function in php called ctype. And? What it does? Well, I see it as a replacement for regular expressions. PHP Ctype function checks for character type. Let’s hear the chars type we can check with that nice function  :

  • alphanumeric characters – ctype_alnum()
  • alphabetic characters      – ctype_alpha()
  • control characters           – ctype_cntrl()
  • numeric characters         – ctype_digit()
  • lowercase characters      – ctype_lower()
  • uppercase characters      – ctype_upper()
  • printable characters        – ctype_print()
  • any printable character but not whitespace or an alphanumeric – ctype_punct()
  • any printable characters but not space – ctype_graph()
  • whitespace characters    – ctype_space()
  • hexadecimal digit characters – ctype_xdigit()

Ok, long list huh? but usefull. I will show you a simple example which of course can be applied for all other ctype() functions :

<?php
//will check if a character type is digit

$toCheck = 1010;

if(ctype_digit($toCheck)) {

print "$toCheck is a number";
}else{

print "$toCheck is NOT a number";
}
?>
This entry was posted in Usefull PHP and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>