[英]XPath - select anchor after some text
我想從以下示例代碼中獲取數據:
<div id="text">
(sd) <a href="http://example.com/somefiledfs.flv">http://example.com/somefiledfs.flv</a>
- 380 kbps
- <a href='/player.swf?config={"clip":{"url":"http://example.com/somefiledfs.flv"}'>Watch</a><br>
(576p) <a href="http://example.com/hgyj.mp4">http://example.com/hgyj.mp4</a>
- 780 kbps
- <a href='/player.swf?config={"clip":{"url":"http://example.com/hgyj.mp4"}'>Watch</a><br>
</div>
我想得到它:
sd - http://example.com/somefiledfs.flv
576p - http://example.com/hgyj.mp4
等等。
某人可以幫忙嗎? 我曾嘗試使用“ // div [@ id ='text'] / a”和祖先/在前,但我無法解決。
這是一個有效的PHP代碼段,基本上遍歷所有鏈接,然后檢查前一個節點是否與sd|576p
匹配(如果需要,在此擴展更多格式...)
<?php
$html = <<<HTML
<div id="text">
(sd) <a href="http://example.com/somefiledfs.flv">http://example.com/somefiledfs.flv</a>
- 380 kbps
- <a href='/player.swf?config={"clip":{"url":"http://example.com/somefiledfs.flv"}'>Watch</a><br>
(576p) <a href="http://example.com/hgyj.mp4">http://example.com/hgyj.mp4</a>
- 780 kbps
- <a href='/player.swf?config={"clip":{"url":"http://example.com/hgyj.mp4"}'>Watch</a><br>
</div>
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$as = $xpath->query("//div[@id='text']/a");
foreach ($as as $a) {
$prev = $a->previousSibling->nodeValue;
if (preg_match("/sd|576p/", $prev, $matches)) {
echo $matches[0]." - ".$a->nodeValue."\r\n";
}
}
?>
這是代碼段的鏈接: https : //eval.in/173038
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.