Jump to content

PREG_MATCH - Alphanumeric check, with two exceptions.


Nhoj

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
[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]

Archived

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

×
×
  • 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.