Jump to content

simple preg_match question


arbitter

Recommended Posts

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.