我想知道你将如何@hotmail.com
删除example@hotmail.com
?
谢谢
function remove_domain($email) {
$v = explode("@", $email);
return $v[0];
}
这将从任何电子邮件 中删除任何域名:
// 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。
$domain = 'gmail.com';
$email = str_replace('@'.$domain, '', $email);
PHP手册页在这里:function.str-replace.php
你可以使用str_replace
,
$email = 'example@hotmail.com';
echo str_replace('@hotmail.com', '', $email); // example
在此处查看文档:http: //php.net/manual/en/function.str-replace.php
我不确定这是否是最好的方法,但您可以尝试以下方法:
$string = "email@hotmail.com";
$new_string = explode("@", $string);
print $new_string[0] // will print 'email'
希望能帮助到你!
或者简单地使用strstr(string $haystack ,mixed $needle [,bool $before_needle = false ]),那么您不需要检查特定域:
$email = 'blabla@hotmail.com';
echo strstr($email, '@', true);