Forum Moderators: coopster

Message Too Old, No Replies

regex question

I thought I understood this stuff

         

4string

4:40 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



I am using this:

if (!eregi ("^[A-Z -\'.]{2,35}$", $name)) {
//error
}

Why does it allow ( ) & @ and some other characters?!

If anyone can help me write or rewrite this statement I'd appreciate it. All I want is letters, hyphens, apostrophes and periods allowed.

timster

4:50 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just escape that hyphen and you should be all set. Without the backslash, that's setting a range of acceptable characters between <space> and apostophe.

I also escape the space character below, for clarity.


if (!eregi ("^[A-Z\ \-\'.]{2,35}$", $name)) {
//error
}

4string

5:16 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Thanks for your help! That makes sense to escape the hyphen. When I use your version it isn't allowing the hyphen now. Why would that be? It allows a forward slash, but not the hyphen.

dcrombie

5:16 pm on Apr 18, 2005 (gmt 0)



Or put the '-' right after the '[' (or just before the ']') and you don't have to escape it.

;)

4string

5:25 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Moving the hyphen worked. How do I disallow the \ it is allowing?

timster

7:21 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, that looks like my bad. Better take the backslashes out -- I usually use preg_match where they are escape characters, but they don't seem to be here. This seems to work:

(!eregi ("^[A-Z '.-]{2,35}$", $name))

4string

12:32 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



I tried that, but then it does not allow apostrophes. It allows the apostrophe and the forward slash when I do:

(!eregi ("^[A-Z .\'-]{2,35}$", $name))

Thanks for everyone's help.

timster

1:36 pm on Apr 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but then it does not allow apostrophes

Really? I just pasted that back into my test page and it did accept apostrophes. Are you sure it's not a "curly quote" thing or another snafu like that?

In any case, I also posted a preg_match version of this; maybe that'll work if the ereg doesn't.

<?php

$name = "What's-up.com.net.edu";
echo "String: $name <hr>";

if (!eregi ("^[A-Z '.-]{2,35}$", $name)) {
echo "Ereg error<br>";
}

if (! preg_match("/^[A-Z\ \'\.\-]{2,35}$/i", $name)) {
echo "Preg error<br>";
}
echo "Done";

?>

Results in:

String: What's-up.com.net.edu 
Done

4string

4:42 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



I'll have to investigate what might be messing with it. Or I'll change it to preg_match. What are the advantages of using one over the other?

timster

6:41 pm on Apr 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What are the advantages of using one over the other?

php.net [us2.php.net] says:


Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().

Beyond that: I myself use preg_match just because I do most of my pattern matching in Perl. But I reckon a lot of people find ereg's syntax easier to work with.