Ninjakreborn Posted May 4, 2007 Share Posted May 4, 2007 What is a simple way to validate a string to make sure it doesn't have any spaces, and doesn't start with a letter.) Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/ Share on other sites More sharing options...
effigy Posted May 4, 2007 Share Posted May 4, 2007 Regular expressions. A preg_match for /\s/ will look for (white)space. A preg_match for /^[A-Za-z]/ will look for a letter at the beginning of a line. Use Unicode properties if the definition of "letter" is broad. Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245468 Share on other sites More sharing options...
Nameless12 Posted May 4, 2007 Share Posted May 4, 2007 without regex you can do the below <?php if (is_numeric($str[0]) && strpos($str, ' ') === false) { //success } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245502 Share on other sites More sharing options...
neel_basu Posted May 4, 2007 Share Posted May 4, 2007 without regex you can do the below <?php if (is_numeric($str[0]) && strpos($str, ' ') === false) { //success } ?> Ya what Nameless12 told is simple and good if you dont want the complexity of Regex Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245505 Share on other sites More sharing options...
effigy Posted May 4, 2007 Share Posted May 4, 2007 Ya what Nameless12 told is simple and good if you dont want the complexity of Regex ...and if by "doesn't start with a letter" you mean "numeric." Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245517 Share on other sites More sharing options...
neel_basu Posted May 4, 2007 Share Posted May 4, 2007 If "doesn't start with a letter" means something other than (\s) e.g. a-zA-Z he needs regex Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245521 Share on other sites More sharing options...
Nameless12 Posted May 4, 2007 Share Posted May 4, 2007 Sorry I assumed it was a number, here is the modified version and no regex is not required. <?php if (!ctype_alpha($str[0]) && strpos($str, ' ') === false) { //success } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245545 Share on other sites More sharing options...
neel_basu Posted May 4, 2007 Share Posted May 4, 2007 no regex is not required.wrong english it should be "no regex is required." Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245546 Share on other sites More sharing options...
Nameless12 Posted May 4, 2007 Share Posted May 4, 2007 no regex is not required.wrong english it should be "no regex is required." I was actually thinking in my head, "and no, regex is not required" but its 3am here and that is why I typed what I did. Try not to be so anal over something so small, this is a programming forum not an english forum! Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245549 Share on other sites More sharing options...
Ninjakreborn Posted May 4, 2007 Author Share Posted May 4, 2007 Thanks for all the help. I have never used C alpha either but I saw the functions. later on I want to go ahead and looking into the C-type library see what all they have to offer. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/50000-solved-php-string-validation/#findComment-245563 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.