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
Link to comment
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]
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.