ChadNomad Posted December 20, 2007 Share Posted December 20, 2007 What's an easy way to check that a string is alpha-numeric? Are there any built-in PHP functions to do this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/82528-solved-easiest-way-to-check-string-is-alpha-numeric/ Share on other sites More sharing options...
thebadbad Posted December 20, 2007 Share Posted December 20, 2007 ctype_alnum Quote Link to comment https://forums.phpfreaks.com/topic/82528-solved-easiest-way-to-check-string-is-alpha-numeric/#findComment-419532 Share on other sites More sharing options...
chigley Posted December 20, 2007 Share Posted December 20, 2007 <?php $string ="abcdefg1"; if(ctype_alnum($string)) { echo "'{$string}' is alphanumeric!"; } else { echo "'{$string}' is NOT alphanumeric!"; } ?> Voila Quote Link to comment https://forums.phpfreaks.com/topic/82528-solved-easiest-way-to-check-string-is-alpha-numeric/#findComment-419533 Share on other sites More sharing options...
redarrow Posted December 20, 2007 Share Posted December 20, 2007 Here a eregi version <?php $word="hi there i am redarrow i am 34"; $result=explode(' ',$word); foreach($result as $x){ if(eregi("^[a-z]{0,100}$",$x)){ echo " <br> $x was alphanumeric <br>"; }else{ echo" <br> sorry $x word was not alphanumeric"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82528-solved-easiest-way-to-check-string-is-alpha-numeric/#findComment-419568 Share on other sites More sharing options...
Pancake Posted December 20, 2007 Share Posted December 20, 2007 Here a eregi version <?php if(eregi("^[a-z]{0,100}$",$x)){ The only issue is that a-z doesn't allow capital letters, and (I think) the {0, 100} is the amount of text that can be sent through... try: if(eregi("^[a-zA-Z0-9]$", $x)) { Or the easier way: if(preg_match('^[[:alnum:]]$', $x) { Quote Link to comment https://forums.phpfreaks.com/topic/82528-solved-easiest-way-to-check-string-is-alpha-numeric/#findComment-419582 Share on other sites More sharing options...
roopurt18 Posted December 20, 2007 Share Posted December 20, 2007 The only issue is that a-z doesn't allow capital letters http://us.php.net/eregi This function is identical to ereg() except that it ignores case distinction when matching alphabetic characters. Quote Link to comment https://forums.phpfreaks.com/topic/82528-solved-easiest-way-to-check-string-is-alpha-numeric/#findComment-419594 Share on other sites More sharing options...
ChadNomad Posted December 20, 2007 Author Share Posted December 20, 2007 Excellent, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/82528-solved-easiest-way-to-check-string-is-alpha-numeric/#findComment-419827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.