我想从这个示例代码中获取数据:
<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