nazly.me

Weblog of Nazly Ahmed

Nazly Ahmed

Nazly Ahmed

Web Developer. PHP Addict. Wordpress Hacker. FOSS Enthusiast. (Micro)Blogger. Photo Hobbyist. Cricket Fanatic. Husband. Dad.

Its really amazing to realise the fact that there is something new to learn everyday.

If I have to validate a string restricting the string can only contain alphanumeric characters I would use regular expressions in the follwing manner..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$a="hello";
$b="12@hhd";
 
echo chkString($a);
echo "<br />";
echo chkString($b);
 
function chkString($str){
if(eregi("^[a-z0-9]+$",$str)){
  return "OK";
}
return "Bad";
}
?>

But I never knew that functions exists specially for these kinda tasks.

I may use ctype_alnum() function for this purpose without using any sort of regular expression.

There are few cool functions available for this sorta tasks..

1
2
3
4
5
6
<?php
$a="hello";
$b="12@hhd";
$a_result_a=ctype_alnum($a);//alphanumeric character check
$b_result_a=ctype_alpha($b);//alphabetic character check
?>
Posted on 4th December 2004 04:30:01