duzun/hQuery.php

Get Attribute value

HelloFromNL opened this issue · 3 comments

How we can get attribute value from this string ?

<div id="cerberus-data-metrics" style="display: none;" data-asin="B00EAHSBV4" data-asin-price="24.55" data-asin-shipping="0" data-asin-currency-code="USD" data-substitute-count="-1" data-device-type="WEB" data-display-code="Asin is not eligible because it has a retail offer" ></div>

following does not work:
$p = $doc->find_text('#cerberus-data-metrics','data-asin-price');

duzun commented

Did you check the Processing the results from README or examples/ folder?

find_text() method doesn't work for attributes.

Here is what you need:

$div = $doc->find('#cerberus-data-metrics');
return $div->attr('data-asin-price');

// or get all the attributes of <div /> as an array
return $div->attr();

Appreciated. Great library.

duzun commented

With the latest update, you could find elements by attributes:

return $doc->find('[data-asin-price]')->attr('data-asin-price');