Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

email

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 using substr()?

like image 737
Bob Cavezza Avatar asked Dec 04 '22 09:12

Bob Cavezza


1 Answers

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.

like image 196
Czechnology Avatar answered Jan 10 '23 04:01

Czechnology