有没有比爆炸更有效的方法来获取电子邮件后缀? (PHP) [英] Is there a more efficient way to get email suffix than explode? (PHP)

查看:72
本文介绍了有没有比爆炸更有效的方法来获取电子邮件后缀? (PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来获取电子邮件后缀的当前代码

Current Code I'm using to get email suffix

$emailarray  = explode('@',$email_address);
$emailSuffix = $emailarray[1];

必须有一个更高效的功能。也许使用 substr()

There's gotta be a more efficient function. Maybe something using substr()?

推荐答案

更短:

$emailSuffix = end(explode('@', $email_address));

但是我认为它没有比这更有效的方法。正则表达式可能会更慢。

But I don't think it gets any significantly more efficient than that. Regex is probably slower.

我做了一些测试,尽管此版本比使用版本快3倍。

I did some testing and although this version was 3 times faster than using the

$a = explode('@', $email_address);
$foo = $a[1];

if (preg_match('~^.+@(.+)$~', $email_address, $reg))
  $foo = $reg[1];

它不符合 strict 标准:


严格标准:仅变量应通过引用传递

Strict Standards: Only variables should be passed by reference



EDIT2



EDIT2

$foo = substr($email_address, strpos($email_address, '@'));

大约与end(explode(。))方法一样快,所以我建议一个。请查看rayman86的答案和评论。

is about as fast as the end(explode(.)) method so I would suggest that one. Please see rayman86's answer and comments.

这篇关于有没有比爆炸更有效的方法来获取电子邮件后缀? (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆