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! 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 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 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"; } } ?> 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) { 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. 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. 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
Archived
This topic is now archived and is closed to further replies.