从php中的电子邮件地址获取用户名 [英] get user name from an E-mail address in php
本文介绍了从php中的电子邮件地址获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,我有一个像这样的字符串:
In PHP, I have a string like this:
$string = "user@domain.com";
我如何仅从电子邮件地址获取用户?是否有任何简单的方法可以在@之前获取值?
How do i get the "user" from email address only? Is there any easy way to get the value before @ ?
推荐答案
假设电子邮件地址有效,则此文本方法应该有效:
Assuming the email address is valid, this textual approach should work:
$prefix = substr($email, 0, strrpos($email, '@'));
直到(但不包括)最后一次 @为止的所有内容
。它使用最后一次出现,因为此电子邮件地址有效:
It takes everything up to (but not including) the last occurrence of @
. It uses the last occurrence because this email address is valid:
"foo\@bar"@iana.org
如果您尚未验证字符串,我建议使用过滤器功能:
If you haven't validated the string yet, I would advice using a filter function:
if (($email = filter_var($email, FILTER_VALIDATE_EMAIL)) !== false) {
// okay, should be valid now
}
这篇关于从php中的电子邮件地址获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文