tags:

views:

54

answers:

4

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

Anyone have an idea?

+2  A: 

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
)
this. __curious_geek
split is deprecated
Col. Shrapnel
removed from the answer. thanks.
this. __curious_geek
+1  A: 

Code:

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

Output:

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

)
Bob Fanger
+1  A: 

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

Ladislav
+2  A: 

Split() is deprecated. explode() is the way to go.

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

Cheers,
Fabian

halfdan