B Todd Poole
B Todd Poole

Reputation: 49

PHP Dom Parse-How to get multiple children

Not completely sure I am asking this correctly but here it goes. I have an html file with following structure:

    <div class="tbody">
    <div class="row">
    <div class="col th">
    <a class="channel_sched_link" href="javascript:void(0)" title="Channel A schedule" data-channelid="9">
   <img src="http://xxxxx/images/tv/A.JPG" width="30" height="20"      alt="Channel A" />Channel A  </a>
  </div>
  <div class="prog_cols">
  <div class="col ts ts_1 prog_802176 ps_0" data-catid="" >
  <span class="prog_name">First Program</span>
  <div class="prog_time">February 24, 2015, 4:00 pm - 6:00 pm</div>
  <a class="btn_watchlist " href="javascript:void(0)" data-progid="802176">  (+) add to watchlist</a>
 <div class="prog_desc">
 This is the first program for channel A.<br/>
 <a class="watchnow" href="http://xxxx/channels/?q=Channel A">Watch Now</a>
 </div>
</div>
<div class="col ts ts_3 prog_802177 ps_1" data-catid="" >
<span class="prog_name">Second Program</span>
<div class="prog_time">February 24, 2015, 6:00 pm - 8:00 pm</div>
<a class="btn_watchlist " href="javascript:void(0)" data-progid="802177">(+)     add to watchlist</a>
<div class="prog_desc">
This is the second program for channel A.<br/>
<a class="watchnow" href="http://www.xxxxx/channels/?q=Channel A">Watch   Now</a>
</div>
</div>
</div>
<a class="watchnow" href="http://xxxx/channels/?q=Channel A">Watch Now</a>
</div>
<div class="row">
<div class="col th">
<a class="channel_sched_link" href="javascript:void(0)" title="Channel B     schedule" data-channelid="1">
 <img src="http://xxxx/images/tv/B.gif" width="30" height="20" alt="Channel B"                                                                                                                           />Channel B    </a>
</div>
<div class="prog_cols">
<div class="col ts ts_1 prog_802210 news ps_0" data-catid="news" >
<span class="prog_name">First Program</span>
<div class="prog_time">February 24, 2015, 5:00 pm - 6:00 pm</div>
<a class="btn_watchlist " href="javascript:void(0)" data-progid="802210">(+)  add to watchlist</a>
<div class="prog_desc">
 First Program Channel B.<br/>
 <a class="watchnow" href="http://xxxxxx/channels/?q=Channel   B">Watch Now</a>
 </div>
 </div>

I am able to parse the prog_name for each channel but only for the first instance of prog_name by using

     $programname = $xpath->query('//span[@class="prog_name"]');

Once I get this I am saving it along with other information to an xml file. How can I parse every prog_name for each channel. I know it probably has something to do with a loop but I am at a loss. Not every channel with have the same number of prog_name.

Upvotes: 0

Views: 82

Answers (1)

alfallouji
alfallouji

Reputation: 1170

This works fine with your html :

$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$childs = $xpath->query('//span[@class="prog_name"]');

foreach ($childs as $child) 
{
    var_dump($child->nodeValue);
}

It returns :

string(13) "First Program"
string(14) "Second Program"
string(13) "First Program"

Upvotes: 1

Related Questions