Hidden string functions

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
?>
Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • FriendFeed
  • Google Bookmarks
  • Technorati
General

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(required)

(required)


Spam protection by WP Captcha-Free