PHP, regex, and quotes
Posted: Wed Jul 23, 2008 11:51 am
I've found some pretty odd behaviour with PHP's regex and quotes... maybe someone can explain this.
Basically I'm trying to build an email address validator that matches up to RFC 2822. For example, any character can start an email address except for a . (period). So my regex looks like this:
ereg("^[a-zA-Z0-9!#\$%\*/\?\|\^\{\}`~&'\+-=_]", '.')
That returns true, and if I remove the minus sign (-), it returns false. Why is that?
On another note, I was messing around and tried this statement:
ereg("^[\\]", '\')
and got this:
Warning: Unexpected character in input: ''' (ASCII=39) state=1
Warning: Unexpected character in input: '\' (ASCII=92) state=1
Warning: Unexpected character in input: ''' (ASCII=39) state=1
Parse error: syntax error, unexpected ')'
Aren't characters in single-quotes considered literal by PHP?
Using ereg("^[\\]", "\") gives Parse error: syntax error, unexpected T_VARIABLE, which is what I would expect.
What is the explanation for all this behaviour?
Basically I'm trying to build an email address validator that matches up to RFC 2822. For example, any character can start an email address except for a . (period). So my regex looks like this:
ereg("^[a-zA-Z0-9!#\$%\*/\?\|\^\{\}`~&'\+-=_]", '.')
That returns true, and if I remove the minus sign (-), it returns false. Why is that?
On another note, I was messing around and tried this statement:
ereg("^[\\]", '\')
and got this:
Warning: Unexpected character in input: ''' (ASCII=39) state=1
Warning: Unexpected character in input: '\' (ASCII=92) state=1
Warning: Unexpected character in input: ''' (ASCII=39) state=1
Parse error: syntax error, unexpected ')'
Aren't characters in single-quotes considered literal by PHP?
Using ereg("^[\\]", "\") gives Parse error: syntax error, unexpected T_VARIABLE, which is what I would expect.
What is the explanation for all this behaviour?