php 使用正则表达式从电子邮件地址中提取用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1798368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using regex to extract username from email address
提问by mrpatg
My string of text looks like this:
我的文本字符串如下所示:
[email protected] (John Doe)
I need to get just the part before the @ and nothing else. The text is coming from a simple XML object if that matters any.
我只需要得到@ 之前的部分,别无其他。如果重要的话,文本来自一个简单的 XML 对象。
The code I have looks like this:
我的代码如下所示:
$authorpre = $key->{"author"};
$re1 = '((?:[a-z][a-z]+))';
if ($c = preg_match_all ("/".$re1."/is", $authorpre, $matches))
{
$author = $matches[1][0];
}
Sometimes the username might have numbers or an underscore before the @ symbol, which is where the regex stops it seems.
有时,用户名可能在 @ 符号之前有数字或下划线,这似乎是正则表达式停止的地方。
回答by Welbog
The regular expression that will match and capture any character until it reaches the @character:
将匹配并捕获任何字符直到到达该@字符的正则表达式:
([^@]+)
That seems like what you need. It'll handle all kinds of freaky variations on e-mail addresses.
这似乎是你所需要的。它将处理电子邮件地址的各种奇怪的变化。
I'm not sure why Ben Jamesdeleted his answer, since I feel it's better than mine. I'm going to post it here (unless he undeletes his answer):
我不知道为什么Ben James删除了他的答案,因为我觉得它比我的要好。我要把它贴在这里(除非他取消删除他的回答):
Why use regex instead of string functions?
$parts = explode("@", "[email protected]"); $username = $parts[0];
为什么使用正则表达式而不是字符串函数?
$parts = explode("@", "[email protected]"); $username = $parts[0];
You don't need regular expressions in this situation at all. I think using explodeis a much better option, personally.
在这种情况下您根本不需要正则表达式。我认为使用explode是一个更好的选择,就个人而言。
As Johannes R?sselpoints out in the comments, e-mail address parsing is rather complicated. If you want to be 100% sure that you will be able to handle any technically-valid e-mail address, you're going to have to write a routine that will handle quoting properly, because both solutions listed in my answer will choke on addresses like "a@b"@example.com. There may be a library that handles this kind of parsing for you, but I am unaware of it.
正如Johannes R?ssel在评论中指出的那样,电子邮件地址解析相当复杂。如果您想 100% 确定您将能够处理任何技术上有效的电子邮件地址,您将不得不编写一个程序来正确处理引用,因为我的答案中列出的两种解决方案都会窒息地址如"a@b"@example.com. 可能有一个库可以为您处理这种解析,但我不知道。
回答by ghostdog74
@OP, if you only want to get everything before @, just use string/array methods. No need complicated regex. Explode on "@", then remove the last element which is the domain part
@OP,如果您只想获取@ 之前的所有内容,只需使用字符串/数组方法。不需要复杂的正则表达式。在“@”上爆炸,然后删除作为域部分的最后一个元素
$str = '"peter@john@doe"@domain.com (John Doe)';
$s = explode("@",$str);
array_pop($s); #remove last element.
$s = implode("@",$s);
print $s;
output
输出
$ php test.php
"peter@john@doe"
回答by Leksat
Maybe this variant is a bit slower than explode(), but it takes only one string:
也许这个变体比explode()慢一点,但它只需要一个字符串:
$name = preg_replace('/@.*?$/', '', $email);
回答by michalzuber
回答by munjal
<?php
$email = '[email protected]';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user; // prints name
?>
回答by webcoder
My suggestion:
我的建议:
$email = '[email protected]';
$username = substr($email, 0, strpos($email, '@'));
// Output (in $username): johndoe
回答by Arkh
I'd go with $author = str_replace(strrchr($authorpre, '@'), '', $authorpre);
我愿意 $author = str_replace(strrchr($authorpre, '@'), '', $authorpre);
回答by markusk
You could start by using mailparse_rfc822_parse_addressesto parse the address and extract just the address specification without any display name. Then, you could extract the part before @with the regexp (.*)@.
您可以首先使用mailparse_rfc822_parse_addresses解析地址并提取没有任何显示名称的地址规范。然后,您可以@使用 regexp提取之前的部分(.*)@。
回答by kante
Use something like this:
使用这样的东西:
list($username, $domain) = explode('@', $email . "@"); // ."@" is a trick: look note below
With this solution you'll have already populated two variables with email address parts in one row.
使用此解决方案,您已经在一行中使用电子邮件地址部分填充了两个变量。
."@": This is made to avoid in short critical errors with the list command and ensure that explodewill produce at least two variables as needed.
."@":这样做是为了避免 list 命令出现短时间的严重错误,并确保explode根据需要至少产生两个变量。
回答by Nabeel MS
I would also like to suggest a non regex solution here as it may be useful in most cases:
我还想在这里建议一个非正则表达式解决方案,因为它在大多数情况下可能有用:
strstr('[email protected]', '@', true)
output:
输出:
n.shah
沙阿

