-6

我想知道你将如何@hotmail.com删除example@hotmail.com

谢谢

4

5 回答 5

2
function remove_domain($email) {
  $v = explode("@", $email);
  return $v[0];
}
于 2012-04-21T23:42:44.370 回答
2

Preg的方法:

这将从任何电子邮件 中删除任何域名:

// Sample email address for testing:
$email = "example@anything.tld";
// Now let's remove @anything.tld:
$email = preg_replace('/@.+/','',$email);
// And then echo results out to see what we got:
echo $email;

所以这里最重要的是这一行,重点关注它:

echo preg_replace('/@.+/', '', 'example@deleteme.com');

它使用正则表达式匹配删除@以至少一个任意字符开头的任何内容。之后它会打印出结果。因此,所有这些都可以通过单行来完成,并且每个域都得到平等的支持(丢弃)。

之后$email只包含"example"删除@anything.tld

这种方式$email可以"my.mail.box@hotmail.com""somebody@mail.ex-ample.com"或者任何你能想象到的方式。

您可以在此处阅读有关正则表达式的更多信息:function.preg- replace.php ,此处:pcre.org或此处:wikipedia/Regular_expression

如果您想使用 str_replace:

$domain = 'gmail.com';
$email = str_replace('@'.$domain, '', $email);

PHP手册页在这里:function.str-replace.php

于 2012-04-22T00:10:38.427 回答
1

你可以使用str_replace

$email = 'example@hotmail.com';
echo str_replace('@hotmail.com', '', $email); // example

在此处查看文档:http: //php.net/manual/en/function.str-replace.php

于 2012-04-21T23:39:06.927 回答
1

我不确定这是否是最好的方法,但您可以尝试以下方法:

$string = "email@hotmail.com";
$new_string = explode("@", $string);
print $new_string[0] // will print 'email'

希望能帮助到你!

于 2012-04-21T23:41:54.693 回答
1

或者简单地使用strstr(string $haystack ,mixed $needle [,bool $before_needle = false ]),那么您不需要检查特定域:

$email = 'blabla@hotmail.com';

echo strstr($email, '@', true);
于 2012-04-21T23:47:54.413 回答