HTML ページからすべての一意の電子メールを配列に取得しようとしています。ファイルは巨大で、メールだけを取得する実際のパターンはありません。
これは GetEmails.html という html の例です --- 実際のファイルには css と、ふるいにかけるためのさらに多くのコードがあります。この例では、電子メールの固有のパターンに注目してください。要するに、すべてがスペースで区切られているわけではありませんが、コンマやセミコロンなどで区切られているものもあります.
<html>
<body>
<p>This is some text and here is an email me@myemail.com and in this text we will see lots of emails like hello@hotmail.com; mike@hello.com, Bill@John.com or even dot orgs too like ed@wisdom.org and all types such as bill@hot.tv,mary@Mary.us and even Obama@yikes.gov some might be bold Ed@Ed.com and some will look like this Email:<strong>Ed@myemail.com</strong>
</p>
<p><u>There will be pages and pages and pages of text to sift thru so get the emails into an array.</u></p>
<p>This is some text and here is an email me@myemail.com and in this text we will see lots of emails like hello@hotmail.com; mike@hello.com, Bill@John.com or even dot orgs too like ed@wisdom.org and all types such as bill@hot.tv,mary@Mary.us and even Obama@yikes.gov some might be bold Ed@Ed.com and some will look like this Email:<strong>Ed@myemail.com</strong> and repeat This is some text and here is an email me@myemail.com and in this text we will see lots of emails like hello@hotmail.com; mike@hello.com, Bill@John.com or even dot orgs too like ed@wisdom.org and all types such as bill@hot.tv,mary@Mary.us and even Obama@yikes.gov some might be bold Ed@Ed.com and some will look like this Email:<strong>Ed@myemail.com</strong></p>
<p> </p>
</body>
</html>
スペースで爆発を使用することを考えましたが、それは機能しない可能性があり、リソースを使いすぎる可能性があります. すべてのメールを配列に入れるのに役立つ単純な関数がphpにあるかどうか疑問に思っています。これが私が試したものです。
<?
$lines = file('GetEmails.html');
foreach ($lines as $line_num => $line) {
/// Finds if line has email.
if (preg_match('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/si', $line))
{
// Puts that line into an array
$line = explode(" " , strip_tags($line));
// Finds if one of the itmes has an @ sign
$fl_array = preg_grep("/@/", $line);
// Puts that email in an array
$TheEmails[] = trim($fl_array);
// Puts only the unique emails an an array
$UniqueEmails= array_unique($TheEmails);
?>
ただし、上記のコードは機能します。私が使用する巨大なファイルは、リソースを不必要に使用しているのではないかと心配しています。また、ed@ed.com,mike@mike.com のようにコンマで区切られた電子メールは考慮されません。
これを行うための最良の方法に関するアイデアはありますか? 少なくとも、スペースなどで区切られた電子メールしか取得できない場合でも、これを行う最善の方法を学ぶことは非常に役立ちます...
これが理にかなっていることを願っています。本当にありがとう!