Author Topic: [SOLVED] Only allow A-Z, 0-9, dashes, underscores, and spaces  (Read 2580 times)

0 Members and 1 Guest are viewing this topic.

Offline pugboyTopic starter

  • Enthusiast
  • Posts: 111
  • Wizard CMS Creator
    • View Profile
    • Wizard CMS
[SOLVED] Only allow A-Z, 0-9, dashes, underscores, and spaces
« on: November 20, 2008, 09:25:27 PM »
I have tried looking at these expressions, and I really don't know how anyone can understand them. Amazing that people can :D

I need to allow only A-Z (upper or lowercase) 0-9 and spaces, dashes, and underscores...

I will be replacing the spaces with dashes however, so that this string is URL friendly and more pleasing to the eye than 'index.php?id=1384'... EDIT: IE is behaving oddly... SUbmitted on accident and I can barely edit... Can anyone point me to a premade expression or help me find a good beginner tutorial on these?
« Last Edit: November 20, 2008, 09:26:44 PM by pugboy »

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: Only allow A-Z, 0-9, dashes, underscores, and spaces
« Reply #1 on: November 20, 2008, 09:58:13 PM »
Google "regular expressions tutorial" or there's a sticky around here some where.


Anyway:


if(preg_match('/^[\dA-Za-z _-]+$/', $str)) {
    //valid
}


That would be one or more of them.  If you want any amount, change the + to a *.
Why doesn't anyone ever say hi, hey, or whad up world?

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: Only allow A-Z, 0-9, dashes, underscores, and spaces
« Reply #2 on: November 20, 2008, 10:22:10 PM »
Or, to shorten it up a bit:

Code: [Select]
<?php
if (preg_match('/^[\w-]+$/'$str) {
    
//good
}
else {
    
//not so good
}
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: Only allow A-Z, 0-9, dashes, underscores, and spaces
« Reply #3 on: November 20, 2008, 10:37:00 PM »
Doesn't \w match all kinds of whitespace?  Like tabs?  (And new lines in multiline mode)
Why doesn't anyone ever say hi, hey, or whad up world?

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: Only allow A-Z, 0-9, dashes, underscores, and spaces
« Reply #4 on: November 20, 2008, 10:43:43 PM »
Doesn't \w match all kinds of whitespace?  Like tabs?  (And new lines in multiline mode)

That's \s.  \w is "word characters", or [A-Za-z0-9_].
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: Only allow A-Z, 0-9, dashes, underscores, and spaces
« Reply #5 on: November 20, 2008, 11:20:53 PM »
Ahhhhhhhh.....  That's why I don't use character classes.  Screw memorization, although \s isn't hard to remember since space.
« Last Edit: November 20, 2008, 11:21:35 PM by corbin »
Why doesn't anyone ever say hi, hey, or whad up world?

Offline DarkWater

  • Freak!
  • Posts: 6,158
  • Gender: Male
    • View Profile
Re: Only allow A-Z, 0-9, dashes, underscores, and spaces
« Reply #6 on: November 20, 2008, 11:26:41 PM »
Ahhhhhhhh.....  That's why I don't use character classes.  Screw memorization, although \s isn't hard to remember since space.

\d, digit
\w, word
\s, space
=P I think they tried to make it pretty easy to remember.
Info:Apache 2.2.4 | PHP 6.0.0-dev, PHP 5.2.4 | Ubuntu 8.04 | Age: 16 | Coding PHP and CSS+(X)HTML: 5-6 years | Feel free to PM me if you need help!
Αν χρειάζεσαι ένας μεταφραστής, μπορέις να μου μιλάς.  Μιλώ καλά.
Quote from: Crayon Violent
If you ask for a banana, would you expect someone to hand you an orange? Or a fork?  No you wouldn't, because you asked for a banana, and you expect them to understand that a banana is not an orange or a fork.  It's the same principle.

Offline corbin

  • Guru
  • Freak!
  • *
  • Posts: 7,951
  • Gender: Male
    • View Profile
Re: Only allow A-Z, 0-9, dashes, underscores, and spaces
« Reply #7 on: November 20, 2008, 11:58:29 PM »
Yeah, I usually use \d just because it's easier than 0-9.  I usually remember \s too.  Not sure why I was thinking the word class included space.
Why doesn't anyone ever say hi, hey, or whad up world?