從 HTML 內容中提取所有 <a> 標籤的 href 屬性及鏈接文本,常用於爬蟲、鏈接檢測、內容分析。
<?php
function extractLinks($html) {
// 匹配 text ,支持單雙引號、屬性順序不固定
$pattern = '/]*href\s*=\s*["\']([^"\']+)["\'][^>]*>(.*?)<\/a>/is';
if (preg_match_all($pattern, $html, $matches)) {
return array_combine($matches[1], $matches[2]); // [url => text]
}
return [];
}
$html = '示例 頁面';
$links = extractLinks($html);
print_r($links);
/*
Array
(
[https://example.com] => 示例
[/page] => 頁面
)
*/
?>
局限: 無法處理嵌套標籤、JS 動態生成、跨行屬性等複雜情況。
<?php
function extractLinksDOM($html, $baseUrl = '') {
libxml_use_internal_errors(true); // 抑制 HTML5 解析警告
$dom = new DOMDocument();
$dom->loadHTML('' . $html); // 強制 UTF-8
libxml_clear_errors();
$links = [];
foreach ($dom->getElementsByTagName('a') as $a) {
$href = $a->getAttribute('href');
$text = trim($a->textContent);
if ($href !== '') {
// 處理相對路徑
if ($baseUrl && !preg_match('/^[a-z]+:/i', $href)) {
$href = rtrim($baseUrl, '/') . '/' . ltrim($href, '/');
}
$links[$href] = $text;
}
}
return $links;
}
// 使用
$html = file_get_contents('https://example.com');
$links = extractLinksDOM($html, 'https://example.com');
print_r($links);
?>
優勢: 正確處理巢狀結構、實體編碼、屬性順序、編碼檢測;可輕鬆擴展提取 title、rel、data-* 等屬性。
<?php
function filterLinks(array $links, array $allowDomains = [], array $denyPatterns = []) {
return array_filter($links, function($text, $url) use ($allowDomains, $denyPatterns) {
// 域名白名單
if ($allowDomains) {
$host = parse_url($url, PHP_URL_HOST);
if (!in_array($host, $allowDomains)) return false;
}
// 黑名單模式(正則)
foreach ($denyPatterns as $pattern) {
if (preg_match($pattern, $url)) return false;
}
return true;
}, ARRAY_FILTER_USE_BOTH);
}
// 去重(保留首次出現文本)
$uniqueLinks = array_unique($links); // 鍵為 URL 自動去重
?>
<?php
function resolveUrl($base, $relative) {
if (preg_match('/^[a-z]+:/i', $relative)) return $relative; // 已是絕對
if ($relative[0] === '/') {
// 根相對路徑
$parts = parse_url($base);
return $parts['scheme'] . '://' . $parts['host'] . $relative;
}
// 當前目錄相對
$baseDir = preg_replace('#/[^/]*$#', '', $base);
return $baseDir . '/' . $relative;
}
?>
symfony/dom-crawler 或 paquettg/php-html-parser 等成熟庫