Jump to content

Archived

This topic is now archived and is closed to further replies.

Nhoj

PREG_MATCH - Alphanumeric check, with two exceptions.

Recommended Posts

I just recently developed a user registration system, not a hard thing at all to make, and i use the code:

[code]preg_match_all('/(?:([a-z]+)|.)/i', $_POST['username'], $matches);
$_POST['username'] = implode('', $matches[1]);[/code]

To strip EVERYTHING that is not alpha numeric from all of the registration inputs and it works exactly how I want it to, however, this also strips the @ sign and periods, which is bad for e-mails.

Is there any way to have it do what it does now except add '@' and '.' to the safe list? This way only alphanumeric characters and those two symbols will remain.

Thanks,
John

Share this post


Link to post
Share on other sites
i would just use preg_replace() instead like this:
[code]
<?php
$String = preg_replace('|[^a-z0-9.@]|', '', $String);
?>
[/code]

make sense?

Share this post


Link to post
Share on other sites
Not sure why I didn't even consider preg_replace, anyway, thanks. I'm using what you suggested with the exception that I added an extra A-Z to allow capital letters.

Share this post


Link to post
Share on other sites
[quote author=Nhoj link=topic=107154.msg429503#msg429503 date=1157574158]
Not sure why I didn't even consider preg_replace, anyway, thanks. I'm using what you suggested with the exception that I added an extra A-Z to allow capital letters.
[/quote]

you could just add the "i" for case insensitivity:
[code]
<?php
$String = preg_replace('|[^a-z0-9.@]|i', '', $String);
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.