advanced_html_dom icon indicating copy to clipboard operation
advanced_html_dom copied to clipboard

find() returns object not array

Open chrisjj opened this issue 8 years ago • 7 comments

This code

$html = str_get_html('<html><table><tr><td><td></body></html>');
var_dump(count(array_slice($html->find('tr'),0,1)));

with advanced_html_dom.php 0.0.11 fails:

Warning: array_slice() expects parameter 1 to be array, object given

whereas with 2016-12-11 simple_html_dom.php (1.5 ($Rev: 196 $)) it succeeds, outputting:

int(1)

chrisjj avatar Dec 11 '16 03:12 chrisjj

Arrays are less useful. For example you can do $html->find('tr')->texts and it will return an array of the texts.

monkeysuffrage avatar Dec 11 '16 03:12 monkeysuffrage

I can see adding a to_array() method though.

monkeysuffrage avatar Dec 11 '16 03:12 monkeysuffrage

Arrays are less useful.

Point taken, but the issue here is that teh drop-in replacement breaks existing code.

For example you can do $html->find('tr')->texts and it will return an array of the texts.

Thanks. OOI, why object to array not object to object?

chrisjj avatar Dec 11 '16 04:12 chrisjj

The idea is that if the method is plural on a NodeList It will map that to each node in the list. So you can do: $html->find('a')->hrefs and it will return all the hrefs on the page. Convenient, right?

monkeysuffrage avatar Dec 11 '16 04:12 monkeysuffrage

I agree that's good.

My thought was just that it seems odd that find() delivers an object (unlike simple__html_dom) and text() accepts an object, but text() delivers an array.

chrisjj avatar Dec 11 '16 12:12 chrisjj

That's a strange way to put it, I think you're misunderstanding something but I'm not sure what.

On Sun, Dec 11, 2016 at 8:29 PM, chrisjj [email protected] wrote:

I agree that's good.

My thought was just that it seems odd that find() delivers an object (unlike simple__html_dom) and text() accepts an object, but text() delivers an array.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/monkeysuffrage/advanced_html_dom/issues/11#issuecomment-266279329, or mute the thread https://github.com/notifications/unsubscribe-auth/AA1afCC-GRMTfEP-AQpfkbB0YfXWf_XWks5rG-ylgaJpZM4LJ00Q .

monkeysuffrage avatar Dec 12 '16 00:12 monkeysuffrage

Duplicate of #9

dolmen avatar Sep 29 '17 16:09 dolmen