I had a situation where I needed to validate an email address that included an apostrophe. It is not widely known that the apostrophe (and a bunch of other symbols for that matter) are valid characters in the official RFC2822 specification for email address formats.
Anyway, I kept getting an error when I tried to add the apostrophe to my character classes in my regex. It gave me a strange error referencing REG_ERANGE. After some googling, I came across this blog post which led me to the answer. The problem is related to the placement of the dash (”-”) character in the regex.
Example 1:
if (ereg("[^a-zA-Z0-9_-.]", $userid)) { echo 'bad'; } else { echo 'good'; }The problem? The dash, or hyphen, being before the period. It thinks it’s a range, like you see in a-z. This may not be a bug, per se, but it’s certainly not smart enough for me.
The solution? Simply put the dash at the end of the regex.
Example 2:
if (ereg("[^a-zA-Z0-9_.-]", $userid)) { echo 'bad'; } else { echo 'good'; }
Thanks for posting this – it is exactly what I just ran into
Yep, me too. Thanks for the help!
Thanks. Googled REG_ERANGE and you were the second result. Thank goodness!