不推荐使用的函数的用法

发布于 2024-08-11 03:50:47 字数 176 浏览 2 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

鹤仙姿 2024-08-18 03:50:47

如前所述,您不应使用 eregi 或任何其他已弃用的函数,因为它们已在下一版本的 PHP 中删除。

查看 PCRE(Perl 兼容正则表达式)的 preg* 函数基于替代方案 (preg_matchpreg_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:

  • The feature has been replaced by a more powerful, alternative feature.
  • The feature is considered extraneous, and will be removed in the future in order to simplify the system as a whole.
  • A future version of the software is planned to make major structural changes, which make it impossible (or impractical) to support older features.
  • Standardization or increased consistency in naming
  • The feature contains a design flaw—frequently a security flaw—and so should be avoided, but existing code depends upon it.
谷夏 2024-08-18 03:50:47

eregeregi 已被弃用,因为它们不支持 Unicode。

回答问题“为什么ereg被弃用?”< /a>,Rasmus Lerdorf 是这样说的:

真正的答案是没有
ereg 函数中的 Unicode 支持,
不管你喜欢与否,世界都在运转
统一码。

请参阅 PHP-DEV 邮件列表上的讨论

ereg and eregi 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:

The real answer is that there is no
Unicode support in the ereg functions,
and like it or not, the world is going
Unicode.

See the discussion on the PHP-DEV mailing list.

庆幸我还是我 2024-08-18 03:50:47

不应使用已弃用的函数;这是因为,虽然它们在当前版本的解释器中可以正常工作,但它们可能(并且将会)从未来版本中完全删除。

因此,如果您在旧项目中使用它们,您也可以将它们保留在其中(但是当迁移到新的 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.

神爱温柔 2024-08-18 03:50:47

它的用法是正确的,但已被弃用。这意味着在 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)

反目相谮 2024-08-18 03:50:47

如果您希望脚本向前兼容,则不应使用已弃用的函数。它们现在有效,但将来可能不起作用。或者,更糟糕的是,他们提出了一个严重的错误,该错误无法轻易修复,但需要进行更大的更改,而修复单个功能无法覆盖该更改。

在您的情况下,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() or preg_match_all() instead.

耳根太软 2024-08-18 03:50:47

正如其他人提到的,您不应该使用已弃用的函数,而应该查看 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.

ˇ宁静的妩媚 2024-08-18 03:50:47

只需将 preg_match 与“i”修饰符一起使用

echo eregi('[\w\.-_]+@[\w\.-_]+\.([a-z]{2,3})', $email) ? 'correct' : 'wrong';
echo preg_match('#[\w\.-_]+@[\w\.-_]+\.([a-z]{2,3})#i', $email) ? 'correct' : 'wrong';

Just use preg_match with 'i' modifier

echo eregi('[\w\.-_]+@[\w\.-_]+\.([a-z]{2,3})', $email) ? 'correct' : 'wrong';
echo preg_match('#[\w\.-_]+@[\w\.-_]+\.([a-z]{2,3})#i', $email) ? 'correct' : 'wrong';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文