在PHP中,如何从一个文本块中提取多个电子邮件地址并将其放入数组中?
我有一个文本块,我想从中提取有效的电子邮件地址并将它们放入数组中。到目前为止我已经在PHP中,如何从一个文本块中提取多个电子邮件地址并将其放入数组中?,php,regex,email,Php,Regex,Email,我有一个文本块,我想从中提取有效的电子邮件地址并将它们放入数组中。到目前为止我已经 $string = file_get_contents("example.txt"); // Load text file contents $matches = array(); //create array $pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'; //regex for patte
$string = file_get_contents("example.txt"); // Load text file contents
$matches = array(); //create array
$pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'; //regex for pattern of e-mail address
preg_match($pattern, $string, $matches); //find matching pattern
但是,我得到的数组只有一个地址。因此,我猜我需要以某种方式循环这个过程。我该怎么做?您的代码几乎完美无瑕,只需将
preg\u match(…)
替换为preg\u match\u all(…)
我知道这不是你问的问题,但我注意到你的正则表达式不接受任何类似“
myemail@office21.company.com
”或带有子域的任何地址。您可以将其替换为以下内容:
/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/
这将拒绝无效的电子邮件(尽管它并不完美)
我还建议您阅读,它非常好而且信息丰富。您非常接近,但是正则表达式不能捕获所有电子邮件格式,并且您不需要指定A-Za-z,您可以使用“I”标志将整个表达式标记为不区分大小写。有一些电子邮件格式的案例(尤其是子域)被遗漏了,但这抓住了我测试的那些
$string = file_get_contents("example.txt"); // Load text file contents
// don't need to preassign $matches, it's created dynamically
// this regex handles more email address formats like a+b@google.com.sg, and the i makes it case insensitive
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
// preg_match_all returns an associative array
preg_match_all($pattern, $string, $matches);
// the data you want is in $matches[0], dump it with var_export() to see it
var_export($matches[0]);
输出:
array (
0 => 'test1+2@gmail.com',
1 => 'test-2@yahoo.co.jp',
2 => 'test@test.com',
3 => 'test@test.co.uk',
4 => 'test@google.com.sg',
)
这个正则表达式将从url或文件中提取所有唯一的电子邮件地址,并在新行中输出每个地址。它将考虑所有子域和前缀后缀问题。找到舒适的使用它
<?
$url="http://example.com/";
$text=file_get_contents($url);
$res = preg_match_all(
"/[a-z0-9]+[_a-z0-9\.-]*[a-z0-9]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})/i",
$text,
$matches
);
if ($res) {
foreach(array_unique($matches[0]) as $email) {
echo $email . "<br />";
}
}
else {
echo "No emails found.";
}
?>
检查此处以获取更多参考信息:这将检测所有邮件地址:
$sourceeee= 'Here are examplr mymail@yahoo.com and my-e.mail@goog.com or something more';
preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/i', $sourceeee, $found_mails);
然后您可以使用
$found\u mails[0]
数组。它对我更有效:
<?php
$content = "Hi my name is Joe, I can be contacted at joe@mysite.com.";
preg_match("/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i", $content, $matches);
print $matches[0];
?>
其他一些不接受以下域:name@example.com.sv
我在上找到它:此函数在不使用正则表达式的情况下运行良好。因此,它速度更快,资源消耗也更低
<?php
function extract_email_addresses($str){
$emails = array();
$str = strip_tags( $str );
$str = preg_replace('/\s+/', ' ', $str);
$str = preg_replace("/[\n\r]/", "", $str);
$remove_chars = array (',', "<", ">", ";", "'", ". ");
$str = str_replace( $remove_chars, ' ', $str );
$parts = explode(' ', $str);
if(count($parts) > 0){
foreach($parts as $part){
$part = trim($part);
if( $part != '' ) {
if( filter_var($part, FILTER_VALIDATE_EMAIL) !== false){
$emails[] = $part;
}
}
}
}
if(count($emails) > 0){
return $emails;
}
else{
return null;
}
}
$string = "Guys, please help me to extract valid sam-ple.1990@gmail.co.uk email addresses from some text content using php
example , i have below text content in mysql database ' Life is more beautiful, and i like to explore lot please email me to sample@gmail.com. Learn new things every day. 'from the above text content i want to extract email address 'sample-x@gmail.com' using php regular expressions or other method.";
$matches = extract_email_addresses( $string );
print_r($matches);
?>
非常感谢您!!我现在快到了。然而,我的输出对我来说有点奇怪。我似乎在数组中获得如下数组:数组([0]=>array([0]=>example@slu.edu)[1]=>Array([0]=>edu))我只寻找一个数组,每个键都包含一个电子邮件地址。@humblehalper preg_replace_all将为原始模式中括号中的任何内容的子匹配创建新的数组元素。在模式的最后一部分是域的周围有括号。要解决此问题,只需在末尾附加$matches=$matches[0]。另外,看看Clay Hinson的答案。他应该得到公认的答案。除非你能解决这个问题,否则这个解决方案是错误的。上述正则表达式中断'email@domain.info'键入最后一部分由4个以上字符组成的电子邮件。你的正则表达式返回'email@domain.inf'. 请修好。而且它先坏了。lastname@domain.be. 只返回'lastname@domain.be“。它可以工作,但无法抓取电子邮件,如:HIDDENFORLOGICALREASONS@cameranh.rs.gov.br,只返回HIDDENFORLOGICALREASONS@cameranh.rs.go. 为了解决这个问题,我们需要将可选参数的数量增加到:“/[a-z0-9\-\+]{1256}+@[a-z0-9\-\.]{1256}+.([a-z]{2,4})(?:\[a-z]{2,3})(?:\[a-z]{2,3}))(?:\[a-z]{2})/i”。请记住,它不适用于子域电子邮件。所以我推荐@Eric Karl response。Buggy确实不能捕捉到我需要的格式。这一个忽略了firstname。lastname@example.com键入电子邮件地址。他们变成了“lastname@example.com“谢谢你的建议!我使用了@Clay Hinson response,但这不适用于子域。我合并了这两个答案,我得到了:“/[a-z0-9\-\+]{1256}+@[a-z0-9\-\.]+.([a-z]{2,4})/I”我在email var上使用了strtolower(),所以它不需要a-z这个正则表达式,来自@Clay Hinson answer的正则表达式不适用。这似乎涵盖了任何类型的电子邮件