开发者

Is there a more efficient way to get email suffix than explode? (PHP)

Current Code I'm using to get email suffix

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

There's gotta be a more efficient function. Maybe something u开发者_StackOverflow中文版sing substr()?


Shorter:

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

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

EDIT

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

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

and

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

it doesn't comply with the strict standards:

Strict Standards: Only variables should be passed by reference

EDIT2

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

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


Using regex, and the preg_match function, you could have something like this :

$email_address = 'hello@world.com';
if (preg_match('/@(.*)$/', $email_address, $matches)) {
    echo $matches[1];
}

Not sure if it's more efficient (it takes more than a single line of code ; and is probably not faster than your solution) -- but it should work quite well.


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


Another way to do it is

$emailSuffix = substr(strstr($email_address, '@'), 1);

Sadly, strstr and strrchr have no "exclude needle" setting, so the substring is required.


Preg_match?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜