views:

118

answers:

2

Hi,

I've a website where users can register. I would like to prevent all special characters, accents, etc in the nickname (used to login).

I use PHP. How can I do that ?

Edit: Another question, can you give me a regular expression (in PHP) who allow ONLY the 26 letters : abcdefghijklmnopqrstuvwxyz and no more ?

Thanks

A: 

You could use PHP's normalizer functions for that.

Sarfraz
+1  A: 
$username = preg_replace('/[^a-z]/i', '', $username);

If you really just mean lowercase a-z, remove the i flag.

In response to comment, the regexp becomes [^a-z0-9]

jasonbar
Only the 26 letters `a`–`z` and no more.
Gumbo
@Gumbo: Corrected after edit.
jasonbar
Ok and if I want also 123456789 ?
Jen Gerfeld
@Jen Gerfeld: See updated answer.
jasonbar
Extra ! So with this line: $username = preg_replace('/[^a-z0-9]/i', '', $username); my $username will can only contain abcdefghijklmnopqrstuvwxyz (lowercase and uppercase) AND 123456789 ? Ok, right ?
Jen Gerfeld
@Jen Gerfeld: a-z0-9 OR *nothing*. If the string had no valid characters, it will now be empty.
jasonbar