Forum Moderators: coopster

Message Too Old, No Replies

Regex for user names

php pattern matching with ereg and preg

         

Nick_W

8:32 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I'm a little lost here: I need to make sure a string does not contain anythin except aphanumerics (and only one space, not two together).

So, nick w is okay butnick<3 spaces>w is not.

nick* is not okay and ~~nick~~ isn't either, see what I mean?

I've got this far:

if (!ereg('[[:alnum:]]', $string)) {
$errors[]="Please use only alphanumeric characters";
}

But it only kinda works, I've read the manual page a couple of times for ereg and it just won't sink in.

Anyone help me out a litte?

Thanks....

Nick

brotherhood of LAN

8:41 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hey Nick,

just use a character class

"'[A-Za-z0-9]'" and you should be OK

//added
stick \s after the 9 if you want the spaces n all

//added again

"'[^A-Za-z0-9\s]'" to delete anything thats NOT inside that class

DrDoc

9:08 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or, use a space instead of \s if you only want to match spaces, not tabs or other whitespace:

[^A-Za-z0-9 ]

DrDoc

9:14 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest using preg_match instead of ereg too...

if (preg_match('[^A-Za-z0-9 ]', $string)) {
$errors[]="Please use only alphanumeric characters";
}

Nick_W

9:43 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the help guys!

Still having a little bother:

if (!preg_match('[^A-Za-z0-9 ]', $string)) {
$errors[]="Please use only alphanumeric characters";
}

Is still assigning the error when I do "nick w"

Basically, I need it to work with usernames, so they might contain a space anywhere in the string (although i'll trim() the ends).

Can you spot what I need here?

Nick

brotherhood of LAN

9:45 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if (!preg_match("'[^A-Za-z0-9 ]'", $string)) {
$errors[]="Please use only alphanumeric characters";
}

just the "s I think

Birdman

9:52 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about this, Nick? Be sure to add the space that the forum software removes so there are two spaces. I think that will do what you want.

if (!preg_match('[^A-Za-z0-9 ]', $string)) {
$errors[]="Please use only alphanumeric characters";
}
if (preg_match [php.net](' ', $string)) {
$errors[]="Too many spaces!";
}

Nick_W

10:00 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that's done it guys!

Beers on me at pubcon ;)

Not sure if I got that last bit Birdman, but between all your great help I've got the little sod sorted!

Many thanks....

Nick

Nick_W

10:06 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ooops, I can still get more than one " " space in a row. Birdman, how's that work there...?

Nick

brotherhood of LAN

10:08 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if (preg_match("'[^A-Za-z0-9]¦\s{2}'", $string)) {
$errors[]="Please use only alphanumeric characters";
}

youll need to change the broken pipe

//changed the code

Nick_W

10:14 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<feeling like a real PIA now...>

Can't get any spaces at all in the string now...

<added>Hold on, you changed the code.....

Nick

Nick_W

10:16 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nope, can't get a space in there at all...

Nick

brotherhood of LAN

10:18 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



add the space after the nine

those!'s at the start of the preg_match always mess with me.....

Nick_W

10:28 pm on Mar 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



100%

Conclusion

Here, thanks to the kind folks in this thread, is how to get pure alphanumeric user names with only one 'space' in a row with PHP.


if (preg_match("'[^A-Za-z0-9 ]¦\s{2}'", $string)) {
// the string is bad - do stuff

}

Thanks again guys...

Nick