开发者

deprecated function's usage

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?


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.


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.


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.


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)


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.


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.


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';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜