开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜