php 获取所有 div 的内容,其中 class 是 X

php get all div's content which class is X

我正在尝试使用 php 获取 div 内容,其中 div 的 class 是 item ajax-product-负载。我的代码是:

$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your HTML
$xpath = new DOMXPath($doc);
// returns a list of all links with rel=nofollow
$nlist = $xpath->query("//div[@class='item ajax-product-load']");
print_r($nlist);

我 return 有多少 div 可用 class item ajax-product-load。但我需要获取 div 的内容。这段代码的结果:

DOMNodeList Object
(
    [length] => 17
)

$xpath->query(...) returns a DOMNodelist object. You need to retrieve an item with $item = $nlist->item(0); and then get the content with $content = $item->textContent;.

$nlist = $xpath->query("//div[@class='item ajax-product-load']");
$item = $nlist->item(0);
$content = $item->textContent;
print_r($content);