How to replace eregi()

好久不见. 提交于 2019-12-24 09:58:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!