sardonicgem Posted May 17, 2008 Share Posted May 17, 2008 Hello folks, I'm trying to check a form field to make sure the user inputs an alphanumeric entry only. The form field is case sensitive. Also I'm not quite sure how preg_match works. php.net indicates: preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. So suppose my string was "Pa55word" preg_match( '[:alnum:]',"Pa55word") (1)So according to the return value conditions, will preg_match stop searching after the first letter P? (2)Does anyone know of a function that would service these types of conditions? If not, I suppose I could write one to store all the characters of the string in an array and then run a match comparison on each element. Although this does not seem optimal at all. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/106012-solved-how-do-constrain-a-form-input-string-to-alphanumeric-characters/ Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 I happen to like ereg() and eregi() more. if (eregi('[a-zA-Z0-9]+', $var) { echo "Good!"; } else { echo "Bad..."; } Obviously, put in your variable and your own stuff in the if {} else {} blocks. Link to comment https://forums.phpfreaks.com/topic/106012-solved-how-do-constrain-a-form-input-string-to-alphanumeric-characters/#findComment-543327 Share on other sites More sharing options...
thebadbad Posted May 17, 2008 Share Posted May 17, 2008 ctype_alnum(). Should be much faster than using RegEx. Link to comment https://forums.phpfreaks.com/topic/106012-solved-how-do-constrain-a-form-input-string-to-alphanumeric-characters/#findComment-543407 Share on other sites More sharing options...
phpzone Posted May 17, 2008 Share Posted May 17, 2008 I think I read ereg will be taken out of PHP 6 core and only available as a compiled option. I'm using preg for all future projects, never know when my code might end up on a PHP 6 server with no ereg available Link to comment https://forums.phpfreaks.com/topic/106012-solved-how-do-constrain-a-form-input-string-to-alphanumeric-characters/#findComment-543437 Share on other sites More sharing options...
sardonicgem Posted May 17, 2008 Author Share Posted May 17, 2008 Thank you all for your help. thebadbad, that function is exactly what I was looking for. Link to comment https://forums.phpfreaks.com/topic/106012-solved-how-do-constrain-a-form-input-string-to-alphanumeric-characters/#findComment-543514 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.