不推荐使用的函数的用法
为什么 PHP 中的某些函数(例如 eregi
)被弃用?我通常使用 eregi
进行电子邮件验证。
我应该使用它,还是有其他函数可以代替它?
Why are certain functions in PHP (such as eregi
) deprecated? I normally use eregi
for email validation.
Should I use it, or is there another function that can be used in its place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如前所述,您不应使用 eregi 或任何其他已弃用的函数,因为它们已在下一版本的 PHP 中删除。
查看 PCRE(Perl 兼容正则表达式)的 preg* 函数基于替代方案 (preg_match 和 preg_match_all 是需要电子邮件验证的函数)。 eregi* 函数是基于 posix 的正则表达式,因此 PCRE 的语法略有不同,但这并不是重大变化。
为什么函数会被弃用? Wikipedia 建议:
As noted, you shouldn't use eregi or any other deprecated functions, as they have been removed in the next version of PHP.
Look at the preg* functions for a PCRE (Perl Compatible Regex) based alternative (preg_match and preg_match_all are the functions needed email validation). The eregi* functions were the posix based regex, so the PCRE has a slightly different syntax but it isn't a major change.
Why would a function become deprecated? Wikipedia suggests:
ereg
和eregi
已被弃用,因为它们不支持 Unicode。回答问题“为什么ereg被弃用?”< /a>,Rasmus Lerdorf 是这样说的:
请参阅 PHP-DEV 邮件列表上的讨论 。
ereg
anderegi
were deprecated because they don't work with Unicode.In answer to the question "Why is ereg being deprecated?", Rasmus Lerdorf had this to say:
See the discussion on the PHP-DEV mailing list.
不应使用已弃用的函数;这是因为,虽然它们在当前版本的解释器中可以正常工作,但它们可能(并且将会)从未来版本中完全删除。
因此,如果您在旧项目中使用它们,您也可以将它们保留在其中(但是当迁移到新的 php 版本时您的项目可能会中断)。
但绝对不要在新项目中使用它们。
Deprecated function should not be used; that is because, while they work correctly in current version of the interpreter, they may (and will) be totally removed from future version.
So if you use them in old projects, you may as well leave them in (but your project will probably break when migrating to a new php version).
But absolutely don't use them in new projects.
它的用法是正确的,但已被弃用。这意味着在 PHP 的未来版本中,此方法可能会被删除。这可能是由于安全、冗余或其他原因。通常,您可以使用其他方法来代替这些方法(例如 eregi -> preg_match)
It's usage is correct, but deprecated. This means that in a future release of PHP this method might get removed. This could be due to security, redundancy or other reasons. Normally there are other methods that you can use instead of these methods (e.g. eregi -> preg_match)
如果您希望脚本向前兼容,则不应使用已弃用的函数。它们现在有效,但将来可能不起作用。或者,更糟糕的是,他们提出了一个严重的错误,该错误无法轻易修复,但需要进行更大的更改,而修复单个功能无法覆盖该更改。
在您的情况下,eregi 将从 PHP 6 中删除,您的脚本将会失败。使用
preg_match()
或preg_match_all()
代替。You shouldn't use deprecated functions if you wish your script to be forward-compatible. They work now, but might not work in the future. Or, even worse, they have presented a critical bug, which cannot be fixed trivially, but warrant a larger change which cannot be covered by fixing a single function.
In your case, eregi will be removed from PHP 6, and your script will simply fail. Use
preg_match()
orpreg_match_all()
instead.正如其他人提到的,您不应该使用已弃用的函数,而应该查看 preg 函数。
特别是对于电子邮件验证,可能值得查看 PHP 的 过滤器 (特别是 filter_var) 函数(如果您的 Web 应用程序将运行在PHP 5.2 及以上版本的服务器。过滤器函数还可用于 url 验证和 IP 地址验证等。
As others have mentioned you should not use deprecated functions, and to look at the preg functions instead.
Specifically for email validation, it might be worth looking at PHP's filter (specifically filter_var) functions if your web application will be running on a server with PHP 5.2 and above. The filter functions can also be used for url validation and validating IP addresses among other things.
只需将
preg_match
与“i”修饰符一起使用Just use
preg_match
with 'i' modifier