如何从字符串中提取所有电子邮件


How to extract all emails from a string?

如何删除?我有 300 000 行的 html 文件我想删除所有内容,所有标签,所有内容,但保留电子邮件。文件中的示例:

ght="20"valign="top"bgcolor="#FFFFFF"><spanclass="style43style44">+995</strong>
<a href="mailto:mail@mail.com">mail@mail.com</a>
:fefw.gefew?chat">rewews</a>

在此文件中是 1000 个电子邮件地址。

试试这个例子:

<?php
$content = 'ght="20"valign="top"bgcolor="#FFFFFF"><spanclass="style43style44">+995</strong>
<a href="mailto:mail@mail.com">mail@mail.com</a>
<a href="mailto:pol@hotmail.it">pol@hotmail.it</a>
john@doe.col-
:fefw.gefew?chat">rewews</a>';
$matches = array(); //create array
$pattern = "/[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z]{2,3})/i";
preg_match_all($pattern, $content, $matches); 
print_r(array_values(array_unique($matches[0])));
?>