phpquery

phpQuery get attr from an empty tag


I have a problem that annoys me pretty much. I'm grabbing some content from a web page using combination of cURL and phpQuery.

In the page I'm fetching, there is following code that determines post rating:

<div class="post">
    <ul id="thumb_ul" class="star-rating" style="width:60px;">
        <li class="current-rating" style="width:0px;"></li>
    </ul>
</div>

I'm using phpQuery as follows:

$pqD = phpQuery::newDocument($buffer);
foreach(pq('div.post') as $li) {
    $rating = pq($li)->find('ul > li.current-rating')->attr('style');
}

Ratings are, obviously, defined in <li> tag, in style attribute. I thought of accessing it with pq($li)->find('ul > li.current-rating')->attr('style') and I expected to get width:0px as a result. Ratings are defined as follows: 0-0, 12-1, 24-2, ..., 60-6.

However, phpQuery doesn's seem very much 'interested' in an empty tag. For every other thing that I'm grabbing from page, it works well.

Any ideas? Thx


Solution

  • UPDATE:

    FULL TEST SOURCE: (copy and test)

    <?php
    require_once('phpQuery/phpQuery.php');
    
    $buffer = '
       <div class="post">
        <ul id="thumb_ul" class="star-rating" style="width:60px;">
            <li class="current-rating" style="width:0px;"></li>
             <li class="current-rating" style="width:6px;"></li>
              <li class="current-rating" style="width:8px;"></li>
        </ul>
    </div>
    ';
    
    $doc = phpQuery::newDocument($buffer);
    foreach ($doc['.post ul li.current-rating'] as $li) {
      $rating .= pq($li)->attr('style');
        // or $rating[] .=  if you need an array()  
    }
    
    echo $rating; //ouput: width:0px;width:6px;width:8px;
    ?>