PHP无法从特定DOM元素检索值[重复]

This question already has an answer here:

I am trying to retrieve content from a p element in this page. As you can see, in the source code there is a paragraph with the content i want:

<p id="qb"><!--
QBlastInfoBegin
    Status=READY
QBlastInfoEnd
--></p>

Actually i want to take the value of the Status. Here is my PHP code.

@$dom->loadHTML($ncbi->ncbi_request($params));
$XPath = new DOMXpath($dom);
$nodes = $XPath->query('//p[@id="qb"]');
$node  = $nodes->item(0)->nodeValue;
var_dump($node))

that returns

["nodeValue"]=> string(0) ""

Any idea ?

Thanks!

</div>

I checked up the site, and it seems you are after the comment inside, you need to add comment() on your xpath query. Consider this example:

$contents = file_get_contents('http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?RID=UY5PPBRH014&CMD=Get');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($contents);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

$comment = $xpath->query('//p[@id="qb"]/comment()')->item(0)->nodeValue;
echo '<pre>';
print_r($comment);

Outputs:

QBlastInfoBegin
    Status=READY
QBlastInfoEnd

Seems that to get comment values you need to use //comment() I'm not too familiar with XPaths so am not too sure on the exact syntax

Sources: https://stackoverflow.com/a/7548089/723139 / https://stackoverflow.com/a/1987555/723139

Update: with working code

<?php

$data = file_get_contents('http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?RID=UY5PPBRH014&CMD=Get');
$dom = new DOMDocument();
@$dom->loadHTML($data);
$XPath = new DOMXpath($dom);
$nodes = $XPath->query('//p[@id="qb"]/comment()');
foreach ($nodes as $comment)
{
    var_dump($comment->textContent);
}