使用 file_get_contents 取得頁面內容,搭配 preg_match_all 與郵箱正則,快速收集公開郵箱地址。
<?php
$url = 'https://example.com';
$html = file_get_contents($url);
function extractEmails($content) {
// RFC 5322 簡化版正則,涵蓋常見格式
$pattern = '/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i';
preg_match_all($pattern, $content, $matches);
return array_unique($matches[0]); // 去重
}
$emails = extractEmails($html);
print_r($emails);
?>
/ # 定界符
[a-z0-9._%+-]+ # 本地部:字母數字、點、底線、百分號、加減號
@ # @ 符號
[a-z0-9.-]+ # 域名:字母數字、點、連字號
\. # 點號
[a-z]{2,} # 頂級域:至少兩字母
/i # 不區分大小寫
<?php
function extractEmailsClean($html) {
// 移除 script、style、noscript 區塊
$html = preg_replace('/<(script|style|noscript)[^>]*>.*?<\/\1>/is', '', $html);
// 移除 HTML 註解
$html = preg_replace('//s', '', $html);
// 提取
$pattern = '/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i';
preg_match_all($pattern, $html, $matches);
return array_unique($matches[0]);
}
?>