سؤال

I have div, and want get 7,89 number, my selector is

<div class="titlePageSprite star-box-giga-star"> 7,89 </div>

$ret['Rating'] = $html->find('div.star-box-giga-star')->content;

but I get empty array, what I doing wrong ?

هل كانت مفيدة؟

المحلول

try this:

$ret['Rating'] = $html->find('div.star-box-giga-star', 0)->innertext;

نصائح أخرى

find() returns a DOMNodeList of ALL elements that match the find parameters, even if there's only one (or none) matching elements. You can tell it specifically which node to return via an optional parameter:

$rating = $html->find('div.star-box-giga-star', 0)
                                              ^^^^---return 1st node (0-based)

or something silly-ish like

$nodes = $html->find(...);
foreach($nodes as $node) {
     $rating = $node->content;
     break;
}

Use XPath directly instead:

   foreach($xpath->query('//div[contains(@class, "star-box-giga-star")]') as $rating) {
      echo $rating->nodeValue, '<br>';
   }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top