Jump to content


Photo

PREG_MATCH - Alphanumeric check, with two exceptions.


  • Please log in to reply
3 replies to this topic

#1 Nhoj

Nhoj
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts
  • LocationClearwater, FL

Posted 06 September 2006 - 07:51 PM

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

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

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
Avid PHP Developer, need some work done? Send a PM ;)

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 September 2006 - 08:05 PM

i would just use preg_replace() instead like this:
<?php
$String = preg_replace('|[^a-z0-9.@]|', '', $String);
?>

make sense?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Nhoj

Nhoj
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts
  • LocationClearwater, FL

Posted 06 September 2006 - 08:22 PM

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.
Avid PHP Developer, need some work done? Send a PM ;)

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 September 2006 - 08:33 PM

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.


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

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users