Giffary
Giffary

Reputation: 3128

How I separate the host example.com from an e-mail address like [email protected] using PHP?

I need to get the host example.com from an e-mail address like [email protected] using PHP?

Anyone have an idea?

Upvotes: 0

Views: 131

Answers (4)

halfdan
halfdan

Reputation: 34244

split() is deprecated; explode() is the way to go:

$parts = explode("@", $email);
echo "Name: " . $parts[0] . "\n";
echo "Host: " . $parts[1];

Upvotes: 2

Ladislav
Ladislav

Reputation: 7283

You can do the following:

$name = '[email protected]';
$result = strstr($name, '@');
echo $result;

return's @gmail.com

or

$name = '[email protected]';
$result = substr(strstr($name, '@'), 1);
echo $result;

return's gmail.com

Upvotes: 1

Bob Fanger
Bob Fanger

Reputation: 29917

Code:

$email = '[email protected]';
$array = explode('@', $email);
var_dump($array);

Output:

array(
  0 => 'user',
  1 => 'gmail.com'

)

Upvotes: 1

this. __curious_geek
this. __curious_geek

Reputation: 43217

http://php.net/manual/en/function.explode.php

<?php
$str = '[email protected]';

// positive limit
print_r(explode('@', $str, 2));

?>

//output

Array
(
    [0] => user
    [1] => gmail.com
)

Upvotes: 2

Related Questions