phpphpquery

phpQuery selecting div inside first li


I have html page what im trying to read(used htmlsql.class.php, but as its too old and outdated, then i have to use phpQuery).

The html markup is:

<ul class="small-block-grid-1 medium-block-grid-2 large-block-grid-3">
<li>
    <div data-widget-type="epg.tvGuide.channel" data-view="epg.tvGuide.channel" id="widget-765574917197" class=" widget-epg_tvGuide_channel">
        <div class="group-box">
            <div class="group-header l-center" data-action="togglePreviousBroadcasts">
                <span class="header-text">
                    <img src="logo.png" style="height: 40px" />
                </span>
            </div>

<div>
    <div class="tvGuide-item is-past">
        <span data-action="toggleEventMeta">
                06:15 what a day
        </span>
        <div class="tvGuide-item-meta">
            Some text.
                <div><a href="/little_charmers?uniqueId=0082201701250415">Näita rohkem</a></div>
        </div>
    </div>
    <div class="tvGuide-item is-current">
        <span data-action="toggleEventMeta">
                06:15 what a day
        </span>
        <div class="tvGuide-item-meta">
            Some text.
                <div><a href="/little_charmers?uniqueId=0082201701250415">Näita rohkem</a></div>
        </div>
    </div>
    <div class="tvGuide-item">
        <span data-action="toggleEventMeta">
                06:15 what a day
        </span>
        <div class="tvGuide-item-meta">
            Some text.
                <div><a href="/little_charmers?uniqueId=0082201701250415">Näita rohkem</a></div>
        </div>
    </div>

</div>
</div>

Then with the previos thing it was fearly easy:

$wsql->select('li');

if (!$wsql->query('SELECT * FROM span')){
    print "Query error: " . $wsql->error; 
    exit;
}

foreach($wsql->fetch_array() as $row){

But i could not read the class so i need to know when the class is current and when its not.

As im new to phpQuery then and reallife examples are hard to find. can someone point me to the right direction.

I would like to have the "span" text and item meta, allso i like to know when the div class is "is-past" or "is-current"


Solution

  • You can find infos about phpQuery here: https://code.google.com/archive/p/phpquery/

    I prefer "one-file" version on top in downloads: https://code.google.com/archive/p/phpquery/downloads

    Simple examples based on your code:

    // for loading files use phpQuery::newDocumentFileHTML();
    // for plain strings use phpQuery::newDocument();
    $document = phpQuery::newDocumentFileHTML('http://domain.com/yourFile.html');
    
    $items = pq($document)->find('.tvGuide-item');
    
    foreach($items as $item) {
        if(pq($item)->hasClass('is-past') === true) {
            // matching past items
        }
    
        if(pq($item)->hasClass('is-current') === true) {
            // matching current items
        }
    
        //  examples for finding elements and grabbing text/attributes
        $span = pq($item)->find('span');
        $text_in_span = pq($span)->text();
    
        $meta = pq($item)->find('.tvGuide-item-meta');
        $link_in_meta = pq($meta)->find('a');
        $href_of_link_in_meta = pq($link_in_meta)->attr('href');        
    }