假設每個具體的文字你正在尋找的地圖只有一個鏈接(這聽起來像你這樣做),你可以建立一個聯合查找陣列。我只是自己遇到了這個需求。這是我如何處理它。這樣你就不需要每次循環所有的鏈接。
function populateOutlines($htmlOutlines)
{
$marker = "courses";
$charSlashFwd = "/";
$outlines = array();
foreach ($htmlOutlines->find("a") as $element)
{
// filter links for ones with certain markers if required
if (strpos($element->href, $marker) !== false)
{
// construct the key the way you need it
$dir = explode($charSlashFwd, $element->href);
$code = preg_replace(
"/[^a-zA-Z0-9 ]/", "", strtoupper(
$dir[1]." ".$dir[2]));
// insert the lookup entry
$outlines[$code] = $element->href;
}
}
return $outlines;
}
// ...stuff...
$htmlOutlines = file_get_html($urlOutlines);
$outlines = populateOutlines($htmlOutlines);
// ...more stuff...
if (array_key_exists($code, $outlines)) {
$outline = $outlines[$code];
} else {
$outline = "n/a";
}
在正常的XPath中,它將是'a [content()=「B」]'。問題是:simplehtmldom支持這個嗎?正常的'DOM'與'DOMXPath'會... – Wrikken