Help with Regex: Alphanumeric AND spaces

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Help with Regex: Alphanumeric AND spaces

Post by matt1019 »

Hi Guys,

Loooooooong time, no post!

I have this if statement using regex, which works just fine, except, I cannot seem to make it accept spaces as well...

Code: Select all

if(!eregi("^([0-9a-z])*$", $address)){
.....
}
How can I modify the regex to let the user input spaces also? [Again, it works just fine with letters and numbers....]

Thanks!

-Matt
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

....add a space. :?

switch to preg_match() too. :)
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

Hi feyd!

Any particular reason why I should switch form eregi to preg_match? (just wanted to know the reason behind it...)

Thanks.

-Matt
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Post by Chalks »

Pretty sure ereg is being phased out of php5.

edit: here's one answer:
Mastering Regular Expressions wrote:However, many of the more modern regex features such as lazy quantifiers, lookaround and Unicode are not supported by the ereg functions.
from here
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

Hi Chalks,

Thanks for the info. I will change my code... as soon as I get it's replacement (preg_match) to work correctly.

This is what I came up with:

Code: Select all

if(!preg_match("/[0-9A-Za-z\s\-]+/",$address)){
... SHOW ERROR ...
}
however.... this is a big problem... as it allows for tags (such as <html> etc. etc. etc.)

How can I fix that?

Thanks,

-Matt
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

It's not checking every character.

Code: Select all

if(!preg_match("/^[0-9A-Za-z\s\-]+$/",$address)){
... SHOW ERROR ...
}
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post by matt1019 »

Hi,

Got it. Just made a simple modification to allow for commas:

Code: Select all

if(!preg_match("/^[0-9A-Za-z\s\,-]+$/", $address)){
... SHOW ERROR ...
}
thanks once again!

-Matt
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Why are you now escaping the comma, and not the dash?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The dash doesn't require escaping.. nor does the comma.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

matt1019 wrote:

Code: Select all

if(!preg_match("/^[0-9A-Za-z\s\,-]+$/", $address)){
... SHOW ERROR ...
}
This will validate, among others, a string consisting entirely of spaces. I'm sure that's not what you intended.
Post Reply