Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
matt1019
Forum Contributor
Posts: 172 Joined: Thu Jul 06, 2006 6:41 pm
Post
by matt1019 » Sat Jul 21, 2007 1:44 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Jul 21, 2007 1:50 pm
....add a space.
switch to
preg_match() too.
matt1019
Forum Contributor
Posts: 172 Joined: Thu Jul 06, 2006 6:41 pm
Post
by matt1019 » Sat Jul 21, 2007 1:52 pm
Hi feyd!
Any particular reason why I should switch form eregi to preg_match? (just wanted to know the reason behind it...)
Thanks.
-Matt
Chalks
Forum Contributor
Posts: 447 Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana
Post
by Chalks » Sat Jul 21, 2007 2:01 pm
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 » Sat Jul 21, 2007 2:09 pm
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
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Sat Jul 21, 2007 2:16 pm
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 » Sat Jul 21, 2007 3:13 pm
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
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Sat Jul 21, 2007 3:39 pm
Why are you now escaping the comma, and not the dash?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Jul 21, 2007 9:42 pm
The dash doesn't require escaping.. nor does the comma.
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Sun Jul 22, 2007 3:03 am
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.