如何通过php从文件中提取特定的字符串(电子邮件)


How to extract a particular string( email )from a file through php

我有一个文本文件,其中包含一些成员关于组织专家的信息。现在我只想从中提取电子邮件地址。

示例文件:

a@a.com jhgvhdhf bahau@gmail.com hdghfd  G@g.com

如何提取所有以@hotmail.com结尾的字符串?如果文件名是foo.txt…

将数据加载到字符串中(我使用$str)并应用电子邮件正则表达式:

$pattern = '/[A-Z0-9._%+-]+@[^'.].*'.[a-z]{2,}/i';
if (preg_match($pattern, $str, $matches)){
    echo $matches[1];
}

正如Guy在下面评论的那样,你可以用将文件加载到一个字符串中

$str = file_get_contents('/path/to/file'); 

编辑:

对于hotmail,您可以将模式更改为:

$pattern = '/[A-Z0-9._%+-]+@hotmail.com/i';