Is there a better way to get a mail suffix than to explode? (Php)

The current code that I use to get the mail suffix

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

There should be a more efficient function. Maybe something is using substr()?

+3
source share
5 answers

In short: D

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

But I do not think that he gets any significant effect. Regex is probably slower.

EDIT

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

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

and

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

It does not meet the standards strictly :

Strict standards: only variables must be passed by reference

EDIT2

$foo = substr($email, strpos($item, '@'));

about as fast as the end (explode (.)) method, so I would suggest it. Please see rayman86's Answer and Comments.

+8

regex preg_match - :

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

, ( , , ), .

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

Another way to do this is

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

Unfortunately, strstrand strrchrit does not matter "to eliminate needle", is therefore required substring.

+1
source
0
source

Source: https://habr.com/ru/post/1794809/


All Articles