dizzy
dizzy

Reputation: 37

Need help to pulling content from PHP Page with Dom / Regex

So far this is my code:

<?php
$start = date("d/m/y", strtotime('today'));
$end = date("d/m/y", strtotime('tomorrow'));

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0"
));
$context = stream_context_create($opts);
$url = "http://www.hot.net.il/PageHandlers/LineUpAdvanceSearch.aspx?text=&channel=506&genre=-1&ageRating=-1&publishYear=-1&productionCountry=-1&startDate=$start&endDate=$end&pageSize=1";
$data = file_get_contents($url, false, $context);

$re = '/LineUpId=(.+\d)/';
preg_match($re, $data, $matches);

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:51.0) Gecko/20100101 Firefox/51.0"
));
$context = stream_context_create($opts);
$url = "http://www.hot.net.il/PageHandlers//LineUpDetails.aspx?lcid=1037&luid=$matches[1]";
$data = file_get_contents($url, false, $context);
echo $data;
?> 

I am trying to prepare a TV Guide for single channel and the current program,

Part of the HTML page:

<div class="GuideLineUpDetailsCenter">
    <a class="LineUpbold">Name of the Show</a>
    <br>
    <div class="LineUpDetailsTime">2018 22:45 - 23:30</div>
    <br>
    <div class="show">Information about the program</div>
    <br>
    <div class="LineUpbold">+14</div>
    <br>
</div>

I want to pull the content and do something like this:

echo $LineUpbold;

echo $LineUpDetailsTime;

echo $show;

echo $LineUpbold;

Upvotes: 1

Views: 23

Answers (1)

Jan
Jan

Reputation: 43179

Use a DOM parser and appropriate xpath queries instead:

<?php

$data = <<<DATA
<div class="GuideLineUpDetailsCenter">
    <a class="LineUpbold">Name of the Show</a>
    <br>
    <div class="LineUpDetailsTime">2018 22:45 - 23:30</div>
    <br>
    <div class="show">Information about the program</div>
    <br>
    <div class="LineUpbold">+14</div>
    <br>
</div>
DATA;

# set up the dom
$dom = new DOMDocument();
$dom->loadHTML($data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

# set up the xpath
$xpath = new DOMXPath($dom);

foreach ($xpath->query("//div[@class = 'GuideLineUpDetailsCenter']") as $container) {
    $name = $xpath->query("a[@class = 'LineUpbold']/text()", $container)->item(0);
    echo $name->nodeValue;

    $details = $xpath->query("div[@class = 'LineUpDetailsTime']/text()", $container)->item(0);
    echo $details->nodeValue;

    # and so on...

}

The code loads your string, searches for divs with the class GuideLineUpDetailsCenter, loops over them and tries to find appropriate children within each div.

Upvotes: 1

Related Questions