arbitter Posted May 8, 2010 Share Posted May 8, 2010 Hello, When a user registers, he is only allowed to register a name containing a-z, A-Z, and 0-9. Now I have searched for hours for decent information about preg_match(), but I jsut don't understand what I need to do. let's say the username = $nick I had: if(!preg_match('/[A-Za-z0-9]/',$nick)) { echo 'Username is not allowed.'; } I thought this was the correct solution, but now I noticed; preg_match only goes to the first letter that is in the array and then outputs a 1. I've searched dozens of sites and can't find out how to do this; how do I make it check every character in $nick? I know this is probably a veeeryyy simple question, but I just can't figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/201106-simple-preg_match-question/ Share on other sites More sharing options...
JAY6390 Posted May 8, 2010 Share Posted May 8, 2010 Use /^[A-Za-z0-9]+$/ Quote Link to comment https://forums.phpfreaks.com/topic/201106-simple-preg_match-question/#findComment-1055082 Share on other sites More sharing options...
cags Posted May 8, 2010 Share Posted May 8, 2010 @arbitter All your pattern does is check if the string contains one of those characters. What JAY6390 has done is add the ^ and $ to anchor the pattern at the start and end of the string respectively and added the + which is a quantifier that says 1 or more. Therefore the pattern will match if the string is one or more characters that only contains those letters/digits. Quote Link to comment https://forums.phpfreaks.com/topic/201106-simple-preg_match-question/#findComment-1055087 Share on other sites More sharing options...
arbitter Posted May 8, 2010 Author Share Posted May 8, 2010 Thanks for the quick response JAY6390, and thanks for further information cags! Didn't really understand what JAY did there first, but I do now. I'm sorry for these easy questions, but I swear I had looked around for a long time. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/201106-simple-preg_match-question/#findComment-1055091 Share on other sites More sharing options...
JAY6390 Posted May 8, 2010 Share Posted May 8, 2010 Here's a good video on regex for beginners that you might find useful http://www.phpvideotutorials.com/regex/ Quote Link to comment https://forums.phpfreaks.com/topic/201106-simple-preg_match-question/#findComment-1055092 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.