PHP ereg 与 preg
我注意到 PHP 正则表达式库中有 ereg 和 preg 之间的选择。有什么区别?其中一个比另一个更快吗?如果是,为什么较慢的那个不被弃用?
在某些情况下,使用其中一种比另一种更好吗?
I have noticed in the PHP regex library there is a choice between ereg and preg. What is the difference? Is one faster than the other and if so, why isn't the slower one deprecated?
Are there any situations where it is better to use one over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,ereg 及其派生函数(ereg_match 等)在 php5 中已被弃用,并在 php6 中被删除,因此您可能最好使用 preg 系列。
preg 用于 Perl 风格的正则表达式,而 ereg 是标准 POSIX 正则表达式。
Well, ereg and its derivate functions (ereg_match, etc) are deprecated in php5 and being removed in php6, so you're probably best going with the preg family instead.
preg is for Perl-style regular expressions, while ereg is standard POSIX regex.
preg 是 Perl 兼容的正则表达式库
ereg 是 POSIX 兼容的正则表达式库,
它们的语法略有不同,并且 preg 在某些情况下稍快一些。 ereg 已被弃用(并且在 php6 中被删除),因此我不建议使用它。
preg is the Perl Compatible Regex library
ereg is the POSIX complient regex library
They have a slightly diffrent syntax and preg is in some cases slightly faster. ereg is deprecated (and it is removed in php6) so I wouldn't recommend that it is used.
关于哪个更快更好的讨论很多。
如果您计划有一天升级到 PHP6,您就已经做出了决定。否则:
普遍的共识是 PCRE 是更好的全面解决方案,但如果您有一个流量很大的特定页面,并且不需要 PHP6,那么可能值得进行一些测试。
例如,来自PHP手册注释:
There is much discussion about which is faster and better.
If you plan on someday advancing to PHP6 your decision is made. Otherwise:
The general consensus is that PCRE is the better all around solution, but if you have a specific page with a lot of traffic, and you don't need PHP6 it may be worth some testing.
For example, from the PHP manual comments:
尽管 ereg 在 PHP 5.3 中已被弃用,但 mb_ereg* 函数却没有。我相信主要原因是 PHP6 正在重建所有 MB/Unicode 支持,因此旧的“常规”ereg 方法毫无用处,因为 mb_ereg 将更新/更好。
我知道它没有回答有关速度的问题,但它确实允许您继续使用 POSIX 和 PCRE。
Even though ereg is deprecated in PHP 5.3, the mb_ereg* functions are not. I believe the main reason for this is because PHP6 is rebuilding all MB/Unicode support and therefore the old "regular" ereg methods are useless since the mb_ereg will be newer/better.
I know it doesn't answer the question regarding speed, but it does allow you to continue using both POSIX and PCRE.
访问 php.net/ereg 显示以下内容:
再往下翻一页,我们读到:
请注意我的强调。
Visiting php.net/ereg displays the following:
Down the page just a bit further and we read this:
Note my emphasis.