问题
I tried looking around and thought i figured it out by using preg_match
, but preg match gives me the error:
Warning: preg_match(): No ending delimiter '^'
here is my original code which php says is depreciated:
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) {
$erroR= "Invalid Email address";
}
can someone explain what the ending delimiter '^' is, and how to add it. thanks@
回答1:
Swap eregi()
with preg_match()
, and add delimiters to the regular expression. I chose the standard /
here, but you can use other characters.
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $_POST['email'])) {
$erroR= "Invalid Email address";
}
The reason you get this warning...
Warning:
preg_match()
: No ending delimiter'^'
...is because preg_match()
expects delimiters, and it assumed the ^
was being used, and it couldn't match a trailing one.
回答2:
Add a /
before and after your expression - you need a delimiter for preg_replace
to tell it where the regex starts and finishes. It can be:
A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).
So this will work:
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/"
来源:https://stackoverflow.com/questions/9204704/how-to-replace-eregi