开发者

how am I screwing up counting the properties in this object?

开发者 https://www.devze.com 2023-03-29 19:19 出处:网络
I\'m using domdocument() to retrieve data from a web page, and I want to count the number of matches:

I'm using domdocument() to retrieve data from a web page, and I want to count the number of matches:

$dom = new DOMDocument();
@$dom->loadHTML($output);
$xpath = new DOMXPath($dom);

$brands = $xpath->query('//li[@class="cp_item"]/a/p[1]'); // get the contents of the first paragraph inside the link

My (likely wrong) understanding is that $brands is an object of which the matches are properties. From PHP.net comments I get the following as a way to count the number of properties in the object.

$count_brands = count((array) $brands);

This yields 0 even though I can then see there are many matches using

foreach ($brands as $brand) {
    echo(trim($tag->nodeValue))
}

Obviou开发者_StackOverflow中文版sly I'm either misunderstanding how the data is getting stored or misapplying the count() method. I'm just learning OO PHP so it's probably something stupid.


This "object" is a DOMNodelist and has a property length which contains the number of items.

$count_brands = $brands -> length;
0

精彩评论

暂无评论...
验证码 换一张
取 消