我如何使用PHP将主机example.com与user@example.com等电子邮件地址分开?

时间:2010-05-22 09:32:56

标签: php email

我需要使用PHP从user@example.com这样的电子邮件地址获取主机example.com吗?

有人有想法吗?

4 个答案:

答案 0 :(得分:2)

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

<?php
$str = 'user@gmail.com';

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

?>

//output

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

答案 1 :(得分:2)

split()已被弃用; explode()是要走的路:

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

答案 2 :(得分:1)

代码:

$email = 'user@gmail.com';
$array = explode('@', $email);
var_dump($array);

输出:

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

)

答案 3 :(得分:1)

您可以执行以下操作:

$name = 'someone@gmail.com';
$result = strstr($name, '@');
echo $result;

return's @ gmail.com

$name = 'someone@gmail.com';
$result = substr(strstr($name, '@'), 1);
echo $result;

return's gmail.com