查询嵌套的HTML标记以返回PHP中它的's属性的值


Query a nested HTML tag to return the value of it's attribute in PHP

什么是最好的方式让PHP查询div类active,然后查询div类content有一个属性song,并有它返回该值作为字符串?

我目前有一些代码看起来像这样:

$xpath = new DOMXPath($doc);
$resulted = $xpath->query('div[@class="active"]');
$active= $resulted->item->query('div[@class="content"]');

但是它一直在运行错误:

Fatal error: Call to a member function query() on a non-object

我的javascript生成的HTML:

<div class="item active" style="display: block; left: 685.5753504672897px; top: 0px; height: 475.3125px; width: 318.84929906542055px; font-size: 100%; z-index: 32768; visibility: visible;">
<canvas class="content landscape" href="#" src="imgs/dead.jpg" title="The Dead Weather - Horehound" id="3" num="0" song="Horehound" origproportion="1.0062305295950156" width="323" height="482"></canvas></div>

我觉得这不是正确的方式去做这件事,但因为我是新来的,我不确定,从我所看到的有很多方法。我只是在寻找最好/最简单的方法。

如果我没猜错的话,你必须这样做

$xpath = new DOMXPath($doc);
$resulted = $xpath->query('div[@class="active"]');
foreach($resulted as $result){
$active= $result->item->query('div[@class="content"]');
}

如果这不是你想做的。请让我知道

刚刚把这个敲掉了:

$doc = new DOMDocument();
$doc->loadXML($html);
$xpath = new DOMXPath($doc);
$resulted = $xpath->query('//div[contains(@class,"active")]/*[contains(@class,"content")]');
echo 'Found items: ' , $resulted->length , "'n";
foreach ($resulted as $result) {
    var_dump($result);
}

我把它弹出到:http://3v4l.org/sug3m

我唯一不明白的是你对Javascript的评论?如何将JS生成的内容反馈给PHP?