替代 eregi() 在 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15046400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative to eregi() in php
提问by idjuradj
So, i was using eregi in my mail script, but as of lately, i get the error that the function is deprecated.
所以,我在我的邮件脚本中使用了 eregi,但最近,我收到了该函数已被弃用的错误。
So, what is the easiest way to replace the following bit of code:
那么,替换以下代码的最简单方法是什么:
if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email'])))?
Any help is appreciated :)
任何帮助表示赞赏:)
回答by Winston
if (!preg_match("/^[A-Z0-9.%-]+@[A-Z0-9.%-]+.[A-Z]{2,4}$/", trim($_POST['email'])))
Using preg_match.
使用 preg_match。
Because ereg_* functions is deprecated in PHP >= 5.3
因为 ereg_* 函数在 PHP >= 5.3 中已弃用
Also for email validation better used filter_var
也用于电子邮件验证更好地使用filter_var
if (!filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL))
echo 'Email is incorrect';

