Hi all,

I need some help with my php script. I'm working on the script to parsing the time from my other script so I can convert the hours into format, example: the hours I parse is 1:00 AM so I can convert it to 20140427010000.

Here is the list of format for 3 channels:

20140427010000

20140427013000

20140427020000

20140427023000

20140428063000

20140428070000

20140428103000

20140428133200

20140428170000

20140429120000

20140429123000

20140429130000

20140429133000

20140429140000

20140426210000

20140426220000

20140426230000

20140427000000

20140427003500

20140427013700

20140427023700

20140427070000

20140428000000
20140428013000

20140428050000

20140428060000

20140428070000

20140429000000

20140429030000

20140429060000

20140429063000

20140429070000

20140426210000

20140426220000

20140426223000

20140426230000

20140427000000

20140427010000

20140427013000

20140427020000

20140427030000

20140428000000

20140428010000

20140428020000

20140428030000

20140428040000

20140429000000

20140429010000

20140429020000

20140429030000

20140429040000[/CODE]

Here is the PHP:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
include ('simple_html_dom.php');

function getState($string)
{
  $ex = explode(" ",$string);
  return $ex[1];
}

$xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '
<tv generator-info-name="www.mysite.com/xmltv">';
$base1 = "http://www.mysite.com/get-listing.php";
$html = file_get_html($base1);    
$i = 0;

foreach($html->find('p[id=links]') as $element)
{
  $program_list[ $count ] = array();
  $id_split = explode("?", $element->plaintext);
  $id_split = explode("&", $link_split[1]);
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];
  $id = explode("id=",$id_split[1]);
  $id = $id[1];

  //channels
  //$channel_test = $html->find('p[id=channels]', 10)->plaintext;
  //echo $channel_test;

  $program_list[$count]['channels'] = $channels;
  $program_list[$count]['id'] = $id;

  $channels_split = explode("?", $element->plaintext);
  $channels_split = explode("&", $channels_split[1]);
  $channel_id = explode("channels=",$channels_split[0]);
  $channel_id = $channel_id[1];
  $my_id = explode("id=",$channels_split[1]);
  $my_id = $my_id[1];

  $channel = urlencode($channel_id);
  $id_1 = urlencode($my_id);
  $html_two = file_get_html("http://www.mysite.com/get-listing.php?channels=" . $channel . "&id=" . $my_id);
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;
  $time2 = $html_two->find('span[id=time2]',0)->plaintext;
  $time3 = $html_two->find('span[id=time3]',0)->plaintext;
  $time4 = $html_two->find('span[id=time4]',0)->plaintext;
  $time5 = $html_two->find('span[id=time5]',0)->plaintext;
  $time6 = $html_two->find('span[id=time6]',0)->plaintext;
  $time7 = $html_two->find('span[id=time7]',0)->plaintext;
  $time8 = $html_two->find('span[id=time8]',0)->plaintext;
  $time9 = $html_two->find('span[id=time9]',0)->plaintext;
  $time10 = $html_two->find('span[id=time10]',0)->plaintext;
  $time11 = $html_two->find('span[id=time11]',0)->plaintext;
  $time12 = $html_two->find('span[id=time12]',0)->plaintext;
  $time13 = $html_two->find('span[id=time13]',0)->plaintext;
  $time14 = $html_two->find('span[id=time14]',0)->plaintext;


  $array = array(
    $time1,
    $time2,
    $time3,
    $time4,
    $time5,
    $time6,
    $time7,
    $time8,
    $time9,
    $time10,
    $time11,
    $time12,
    $time13,
    $time14,
  );

  // Save the output format
  $DATE_FORMAT_STRING = "YmdHis";

  // GET the current STAGE
  $current_state = getState($array[0]);
  $offset = 0;

  foreach($array as $time)
  {
    // Get the item state.  
    $this_state = getState($time);

    // check if we past a day? 
    if($current_state == "PM" && $this_state == "AM")
    {
      $offset++;
    }
    $this_unix = strtotime($time) + (60 * 60 * 24 * $offset);
    echo date($DATE_FORMAT_STRING, $this_unix);
    echo "<br></br>";
    $current_state = $this_state;
  }
}
?>

I want to know how do you get each of those format to output them in the xml start="" and end="" like this?

<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.mysite.com/XML">
  <programme channel='test start='20140427010000' stop='20140427013000'>
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='test start='20140427013000' stop='20140427020000'>
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='test start='20140427020000' stop='20140427023000'>
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='test start='20140427023000' stop='20140428063000'>
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='test start='20140428063000' stop='20140428070000'>
    <title lang="en"></title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  </tv>

I tried to use the values, but I will get the wrong format and the format will get mess up if I use the values.

Does anyone know how I can get the format for per channel using with the values if that is possible?

diafol, I'm sorry but I'm using this as I have found the solution. I'm parsing the time especially hours so i can convert it to format. This is the only working one that I can use for now.

In my code, I can generating the date format when I'm parsing the time, but I cannot be able to stored the values into arrays as I would get the wrong format from different arrays. I want to know how I can get the format into arrays using the values when I convert the time into format in each time I send request to url to get the time?

Member Avatar for diafol

I'm confused. Give a sample of your input data. Are you just trying to create an XML document from another with just replacing the time format?

This isn't right...

<programme channel='test start='20140427010000' stop='20140427013000'>

Should it be...

<programme channel='test' start='20140427010000' stop='20140427013000'>

??

/?EDIT

Ok, I see you're loading a php page. Perhaps if you supplied the url we could see it.

Is this you?

http://stackoverflow.com/questions/22882636/how-to-get-the-element-in-arrays

Ok, here is the example of what I'm trying to achieve. I have the list of 3 urls, so I'm sending the request to each url which I will get the hours and I will convert it to format.

Here is the input:

For Url one:

7:30 AM
9:30 AM

For Url two:

9:00 AM

11:00 AM

For Url three:

9:00 AM

10:00 AM

In that way, I will convert the hours of 7:30 AM into this 20140426073000, for 9:30AM i will convert it to 20140426093000. When I send the request to each url to get the hours and converting the hours into format, I will get the three same input of format when I run through the loops.

Here is the input:

20140426073000
20140426093000

20140426073000
20140426093000

20140426073000
20140426093000

It should be looks like this:

20140426073000
20140426093000

20140426009000
20140426011000

20140426090000
20140426100000

Here is the code:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
include ('simple_html_dom.php');

function getState($string)
{
  $ex = explode(" ",$string);
  return $ex[1];
}

$xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '
<tv generator-info-name="www.mysite.com/xmltv">';
$base1 = "http://www.mysite.com/get-listing.php";
$html = file_get_html($base1);      
$i = 0;
$count = 0;

foreach($html->find('p[id=links]') as $element)
{
  $program_list[ $count ] = array();
  $id_split = explode("?", $element->plaintext);
  $id_split = explode("&", $link_split[1]);
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];
  $id = explode("id=",$id_split[1]);
  $id = $id[1];

  //channels
  //$channel_test = $html->find('p[id=channels]', 10)->plaintext;
  //echo $channel_test;

  $program_list[$count]['channels'] = $channels;
  $program_list[$count]['id'] = $id;

  $channels_split = explode("?", $element->plaintext);
  $channels_split = explode("&", $channels_split[1]);
  $channel_id = explode("channels=",$channels_split[0]);
  $channel_id = $channel_id[1];
  $my_id = explode("id=",$channels_split[1]);
  $my_id = $my_id[1];

  $channel = urlencode($channel_id);
  $id_1 = urlencode($my_id);
  $html_two = file_get_html("http://www.mysite.com/get-listing.php?channels=" . $channel . "&id=" . $my_id);
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;
  $time2 = $html_two->find('span[id=time2]',0)->plaintext;
  $time3 = $html_two->find('span[id=time3]',0)->plaintext;


  $array = array(
    $time1,
    $time2,
    $time3,
  );

  // Save the output format
  $DATE_FORMAT_STRING = "YmdHis";

  // GET the current STAGE
  $current_state = getState($array[0]);
  $offset = 0;

  foreach($array as $time)
  {
    // Get the item state.  
    $this_state = getState($time);

    // check if we past a day? 
    if($current_state == "PM" && $this_state == "AM")
    {
      $offset++;
    }
    $this_unix = strtotime($time) + (60 * 60 * 24 * $offset);
    $values[] = date($DATE_FORMAT_STRING, $this_unix);
    //echo date($DATE_FORMAT_STRING, $this_unix);
    //echo "<br></br>";

  }
  echo $values[0];
  echo "<br>";
  echo $values[1];
  echo "<br></br>";
  $current_state = $this_state;
}
?>

Do you know how I can get the different format in each time when I send the request to each url when I parse the hours to convert it to format so I would not get the wrong format from different url?

When I tried to get the format for each url that I send request, I will get the same input of format when I run through the loops.

Member Avatar for diafol

What do you mean 'same format'? Do you mean the same time?

OK - I see from the example that the datetimes are replaying.

I don't see $count being incremented.

Anyway, I'll say it again - offsetting with seconds is a really BAD idea. It will mess up with daylight saving and possibly leap years.

Yeah, I will get the same time when I convert the time to format. Do you know how to fix it?

I don't have any problem with the offsetting. I'm getting the right daylight saving with seconds and hours. If I have a problem, I will let you know.

Member Avatar for diafol

Do you know how to fix it?

I said...

I don't see $count being incremented.

You need $count++ in the end of your loop.

i have added the $count++ in the end of my loop, but i'm still getting the same result.

foreach($html->find('p[id=links]') as $element)
{
  $program_list[ $count ] = array();
  $id_split = explode("?", $element->plaintext);
  $id_split = explode("&", $link_split[1]);
  $channels = explode("channels=",$id_split[0]);
  $channels = $channels[1];
  $id = explode("id=",$id_split[1]);
  $id = $id[1];

  //channels
  //$channel_test = $html->find('p[id=channels]', 10)->plaintext;
  //echo $channel_test;

  $program_list[$count]['channels'] = $channels;
  $program_list[$count]['id'] = $id;

  $channels_split = explode("?", $element->plaintext);
  $channels_split = explode("&", $channels_split[1]);
  $channel_id = explode("channels=",$channels_split[0]);
  $channel_id = $channel_id[1];
  $my_id = explode("id=",$channels_split[1]);
  $my_id = $my_id[1];

  $channel = urlencode($channel_id);
  $id_1 = urlencode($my_id);
  $html_two = file_get_html("http://www.mysite.com/get-listing.php?channels=" . $channel . "&id=" . $my_id);
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;
  $time2 = $html_two->find('span[id=time2]',0)->plaintext;
  $time3 = $html_two->find('span[id=time3]',0)->plaintext;
  $time4 = $html_two->find('span[id=time4]',0)->plaintext;

  $array = array(
    $time1,
    $time2,
    $time3,
    $time4,
  );

  // Save the output format
  $DATE_FORMAT_STRING = "YmdHis";

  // GET the current STAGE
  $current_state = getState($array[0]);
  $offset = 0;

  foreach($array as $time)
  {
    // Get the item state.  
    $this_state = getState($time);

    // check if we past a day? 
    if($current_state == "PM" && $this_state == "AM")
    {
      $offset++;
    }
    $this_unix = strtotime($time) + (60 * 60 * 24 * $offset);
    $values[] = date($DATE_FORMAT_STRING, $this_unix);
    //echo date($DATE_FORMAT_STRING, $this_unix);
    //echo "<br></br>";

  }
  echo $values[0];
  echo "<br>";
  echo $values[1];
  echo "<br></br>";
  $current_state = $this_state;
  $count++;
}

Here is the input:

20140426133000
20140426153000

20140426133000
20140426153000

20140426133000
20140426153000

20140426133000
20140426153000

Here is the input:

http://www.mysite.com/get-listing.php?channels=ABC+FAMILY&id=101
http://www.mysite.com/get-listing.php?channels=CBS&id=102
http://www.mysite.com/get-listing.php?channels=CNN+USA&id=103

I think I would need to create foeach url loop to send request to get the time from each url, because I will keep getting the same time for each array when I will only get the time from the first url.

However, I have tried this:

  $html_two = file_get_html("http://www.mysite.com/get-listing.php?channels=" . $channel . "&id=" . $my_id);
  $time1 = $html_two->find('span[id=time1]',0)->plaintext;
  echo $time1;
  echo "<br>";

Here is the input:

1:30 PM 
3:00 PM 
3:00 PM

So how I can get each of those time before I can convert it to format?

I have sent you a PM, please check it.

Member Avatar for diafol

In the end I used DomDocument - simple_html_dom.php had memory leaks that I couldn't track down - well not without spending more time on it.

Here's a class which may help. Rough and ready though...

<?php
ini_set('max_execution_time', 300);

//VARIABLES
$template =
  "<programme channel='__CHANNEL__' start='__START__' stop='__STOP__'>
    <title lang=\"en\">__TITLE__</title>
    <sub-title lang=\"en\">__SUBTITLE__</sub-title>
    <desc lang=\"en\">__DESC__</desc>
    <category lang=\"en\">__CATEGORY__</category>
  </programme>";


$base = 'http://67.23.248.61/~ytestbox/get-listing.php';

//INITALISE AND RENDER
$x = new tvXml($base);
$x->render($template);


//THE CLASS
class tvXml
{
    private $baseUrl;
    private $domDoc;
    private $dt;
    private $previousTime;
    private $result;

    public function __construct($baseUrl)
    {
        $this->baseUrl = $baseUrl;
        $this->domDoc = new DOMDocument();
        $this->domDoc->strictErrorChecking = false;
        $this->domDoc->recover=true;
        @$this->domDoc->loadHTMLFile($baseUrl);
        $this->result = $this->extract_links();
    }

    //trivial function for making some of your urls safer
    private function respace($url)
    {
        return str_replace(" ", "%20",$url);
    }

    //return a sorted array of data from all the channels
    public function extract_links()
    {
        $links = $this->domDoc->getElementsByTagName('a');
        $data = array();
        foreach($links as $link)
        {
            if(strstr($link->getAttribute('href'), $this->baseUrl))
            { 
                $url = $this->respace($link->getAttribute('href'));
                $sdoc = new DOMDocument();
                $sdoc->strictErrorChecking = false;
                $sdoc->recover=true;
                @$sdoc->loadHTMLFile($url);
                $spans = $sdoc->getElementsByTagName('span');
                $query = parse_url($url)['query'];
                $bits = explode("&", $query)[0];
                $channel = urldecode(explode("=",$bits)[1]);
                $data = array_merge($data, array_values($this->strip_items($channel, $spans))); 
            }
        }

        uasort($data, function($a, $b) { return strnatcmp($a["time"], $b["time"]); });
        return $data;
    }

    //simply echo the XML snippet - equally change this to return instead of eacho to build an XML document.
    public function render($template, $channel='__CHANNEL__', $start='__START__', $stop='__STOP__', $title='__TITLE__', $subtitle='__SUBTITLE__', $desc = '__DESC__', $category = '__CATEGORY__')
    {
        $output = '';
        foreach($this->result as $result)
        {
            if(!isset($result['subtitle'])) $result['subtitle'] = '';
            if(!isset($result['desc'])) $result['desc'] = '';
            if(!isset($result['category'])) $result['category'] = '';

            $output .= "\n" . str_replace(array($channel, $start, $stop, $title, $subtitle, $desc, $category), array($result['channel'], $result['time'], $result['time2'], $result['title'], $result['subtitle'], $result['desc'], $result['category']), $template);           
        }
        echo $output;
    }



    //Pull out items from pages and get times into correct formats into an array
    private function strip_items($channel, $spans, $time="time", $title="title", $subtitle=NULL, $desc=NULL, $category=NULL)
    {
        $itemArray = array('time'=>$time, 'title'=>$title);
        if($subtitle) $itemArray = array_merge($itemArray, array('subtitle'=>$subtitle));
        if($desc) $itemArray = array_merge($itemArray, array('desc'=>$desc));
        if($category) $itemArray = array_merge($itemArray, array('category'=>$category));

        $data = array();
        $this->dt = new DateTime();
        $this->previousTime = $this->dt->format('Hi'); 
        foreach($spans as $span)
        {
            $id = $span->getAttribute('id');
            foreach($itemArray as $key=>$value)
            {
                if(substr($id,0,strlen($value)) == $value)
                { 
                    $num = substr($id,strlen($value));
                    $data[$num][$key] = ($key == 'time') ? $this->retime($span->nodeValue) : $span->nodeValue;
                }
            }
        }
        $numCount = count($data);

        for($i=1; $i<=$numCount; $i++)
        {
            $data[$i]['time2'] = (isset($data[($i + 1)]['time'])) ? $data[($i + 1)]['time'] : '';   
            $data[$i]['channel'] = $channel;
        }

        return $data;
    }

    //Just return the Hi format for time and set the previous time 
    private function retime($timeAMPM)
    {
        $tmpDT = new DateTime(date('Y-m-d ') . $timeAMPM);
        $conv = $tmpDT->format('Hi');
        if($conv < $this->previousTime) $this->dt->add(new DateInterval('P1D'));
        $this->previousTime = $conv;
        return $this->dt->format('Ymd') . $conv;
    }
}
?>

Thank you very much for this, but there are two things that are something wrong with the code. The "<channel id='"101 ABC FAMILY"'>";, </channel> and </tv> is missing.

The code you wrote, it have all of these programme messing up.

Here it is:

<programme channel='BRAVO7' start='201404282000' stop='201404282100'>
    <title lang="en">The Real Housewives of Atlanta -  Reunion Part Two</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='Animal Planet' start='201404282000' stop='201404282100'>
    <title lang="en">River Monsters -  River of Blood</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='BRAVO2' start='201404282000' stop='201404282100'>
    <title lang="en">The Real Housewives of Atlanta -  Reunion Part Two</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='CBS' start='201404282000' stop='201404282100'>
    <title lang="en">60 Minutes</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='BRAVO4' start='201404282000' stop='201404282100'>
    <title lang="en">The Real Housewives of Atlanta -  Reunion Part Two</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='BRAVO1' start='201404282000' stop='201404282100'>
    <title lang="en">The Real Housewives of Atlanta -  Reunion Part Two</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='CNN USA' start='201404282000' stop='201404282100'>
    <title lang="en">Anthony Bourdain Parts Unknown -  Las Vegas</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='USA Network' start='201404282000' stop='201404282100'>
    <title lang="en">Law & Order: Special Victims Unit -  Criminal Hatred</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='SPIKE' start='201404282000' stop='201404282100'>
    <title lang="en">Bar Rescue -  When Life Doesn't Hand You Lemons</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='ABC FAMILY' start='201404282000' stop='201404282200'>
    <title lang="en">Mirror Mirror</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>
<programme channel='BRAVO USA' start='201404282000' stop='201404282100'>
    <title lang="en">The Real Housewives of Atlanta -  Reunion Part Two</title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"></desc>
    <category lang="en"></category>
  </programme>

It should show like this:

<?xml version="1.0" encoding="UTF-8" ?>
    <tv generator-info-name="www.mysite.com/XML">
    <channel id='"101 ABC FAMILY"'>
      <programme channel='101 ABC+FAMILY' start='20140425180000' stop='20140425183000'>
        <title lang="en">The Middle -  Thanksgiving III </title>
        <sub-title lang="en"></sub-title>
        <desc lang="en"> </desc>
        <category lang="en"></category>
      </programme>
      <programme channel='101 ABC+FAMILY' start='20140425183000' stop='20140425190000'>
        <title lang="en">The Middle -  A Christmas Gift </title>
        <sub-title lang="en"></sub-title>
        <desc lang="en"> </desc>
        <category lang="en"></category>
      </programme>
      <programme channel='101 ABC+FAMILY' start='20140425190000' stop='20140425210000'>
        <title lang="en">Liar Liar </title>
        <sub-title lang="en"></sub-title>
        <desc lang="en"> </desc>
        <category lang="en"></category>
      </programme>
      <programme channel='101 ABC+FAMILY' start='20140425210000' stop='20140425230000'>
        <title lang="en">Bruce Almighty </title>
        <sub-title lang="en"></sub-title>
        <desc lang="en"> </desc>
        <category lang="en"></category>
      </programme>
    </channel>
    <programme channel='102 CBS' start='20140425180000' stop='20140425203000'>
    <title lang="en">Local Programming </title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <channel id='"102 CBS"'>
  <programme channel='102 CBS' start='20140425203000' stop='20140425210000'>
    <title lang="en">CBS Evening News With Scott Pelley </title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='102 CBS' start='20140425210000' stop='20140425220000'>
    <title lang="en">Unforgettable -  East of Islip </title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='102 CBS' start='20140425220000' stop='20140425230000'>
    <title lang="en">Hawaii Five-0 -  Peepee Kainaka </title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='102 CBS' start='20140425230000' stop='20140426000000'>
    <title lang="en">Blue Bloods -  Custody Battle </title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='102 CBS' start='20140426000000' stop='20140426123500'>
    <title lang="en">Local Programming </title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='102 CBS' start='20140426123500' stop='20140426013700'>
    <title lang="en">Late Show With David Letterman </title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  <programme channel='102 CBS' start='20140426013700' stop='20140426023700'>
    <title lang="en">The Late Late Show With Craig Ferguson </title>
    <sub-title lang="en"></sub-title>
    <desc lang="en"> </desc>
    <category lang="en"></category>
  </programme>
  </channel>

And you have forgot to include them:

header("Content-Type: text/xml");
echo $xml;
$handle = fopen("myChannel.xml", "w"); 
fwrite ($handle, $xml);
Member Avatar for diafol

And you have forgot to include them:

No I didn't - that's up to you - you output it any way you want to. I'm not following a brief here, just giving you a big hand.

Anyway, there's a problem with the render when '&' is in the title, so you need to htmlentities() all text.

I sorted the programmes by time, because you noted in a previous post that the times needed to be in order. Placing it in 'channel' would have been hell of a lot easier.

Anyway, I'm going to bed. I may have a look at it tomorrow.

Member Avatar for diafol

BTW - You make no mention of the 'channel' tag in your first post - your XML example is pretty much as I've output. I'll spend 10 minutes on it, then it's up to you to do what you want.

//EDIT

ini_set('max_execution_time', 300);

require "tv.class.php";

//VARIABLES
$template =
  "\n\t\t<programme channel='__CHANNEL__' start='__START__' stop='__STOP__'>\n\t\t\t<title lang=\"en\">__TITLE__</title>\n\t\t\t<sub-title lang=\"en\">__SUBTITLE__</sub-title>\n\t\t\t<desc lang=\"en\">__DESC__</desc>\n\t\t\t<category lang=\"en\">__CATEGORY__</category>\n\t\t</programme>\n";

$base = 'http://67.23.248.61/~ytestbox/get-listing.php';

//INITALISE AND RENDER
$x = new tvXml($base);

$data = $x->render($template);

//=====Display=====

/*
header("Content-Type: text/xml");
echo $data;
*/

//=====Save to file=====

/*
$filename = 'tv.xml';
file_put_contents($filename, $data);
*/

Here's the modified class (tv.class.php)...

<?php
class tvXml
{
    private $baseUrl;
    private $domDoc;
    private $dt;
    private $previousTime;
    private $result;

    public function __construct($baseUrl)
    {
        $this->baseUrl = $baseUrl;
        $this->domDoc = new DOMDocument();
        $this->domDoc->strictErrorChecking = false;
        $this->domDoc->recover=true;
        @$this->domDoc->loadHTMLFile($baseUrl);
        $this->result = $this->extract_links();
    }

    //trivial function for making some of your urls safer
    private function respace($url)
    {
        return str_replace(" ", "%20",$url);
    }

    private function respacePlus($channelName)
    {
        return str_replace(" ", "+",$channelName);
    }

    //return a sorted array of data from all the channels
    public function extract_links()
    {
        $links = $this->domDoc->getElementsByTagName('a');
        $data = array();
        foreach($links as $link)
        {
            if(strstr($link->getAttribute('href'), $this->baseUrl))
            { 
                $url = $this->respace($link->getAttribute('href'));
                $sdoc = new DOMDocument();
                $sdoc->strictErrorChecking = false;
                $sdoc->recover=true;
                @$sdoc->loadHTMLFile($url);
                $spans = $sdoc->getElementsByTagName('span');
                $query = parse_url($url)['query'];
                $params = explode("&", $query);
                $channelNum = explode("=", $params[1])[1];
                $channelName = urldecode(explode("=",$params[0])[1]);
                $data = array_merge($data, $this->strip_items($channelName, $channelNum, $spans)); 
            }
        }

        //uasort($data, function($a, $b) { return strnatcmp($a["time"], $b["time"]); });
        return $data;
    }

    //simply echo the XML snippet - equally change this to return instead of eacho to build an XML document.
    public function render($template, $channel='__CHANNEL__', $start='__START__', $stop='__STOP__', $title='__TITLE__', $subtitle='__SUBTITLE__', $desc = '__DESC__', $category = '__CATEGORY__')
    {
        $output = '<?xml version="1.0" encoding="UTF-8" ?>';
        $output .= "\n<tv generator-info-name=\"www.mysite.com/xmltv\">";
        foreach($this->result as $sChannel=>$resultArray)
        {
            $output .= "\n\t<channel id=\"$sChannel\">";
            foreach($resultArray as $result)
            {           
                if(!isset($result['subtitle'])) $result['subtitle'] = '';
                if(!isset($result['desc'])) $result['desc'] = '';
                if(!isset($result['category'])) $result['category'] = '';

                $output .= str_replace(array($channel, $start, $stop, $title, $subtitle, $desc, $category), array(htmlentities($result['channel']), $result['time'], $result['time2'], htmlentities($result['title']), htmlentities($result['subtitle']), htmlentities($result['desc']), htmlentities($result['category'])), $template); 
            }
            $output .= "\n\t</channel>";
        }
        $output .="\n</tv>";
        return $output;
    }

    //Pull out items from pages and get times into correct formats into an array
    private function strip_items($channelName, $channelNum, $spans, $time="time", $title="title", $subtitle=NULL, $desc=NULL, $category=NULL)
    {
        $itemArray = array('time'=>$time, 'title'=>$title);
        if($subtitle) $itemArray = array_merge($itemArray, array('subtitle'=>$subtitle));
        if($desc) $itemArray = array_merge($itemArray, array('desc'=>$desc));
        if($category) $itemArray = array_merge($itemArray, array('category'=>$category));

        $channelKey = $channelNum . ' ' . $channelName;

        $data = array();
        $this->dt = new DateTime();
        $this->previousTime = $this->dt->format('Hi'); 
        foreach($spans as $span)
        {
            $id = $span->getAttribute('id');
            foreach($itemArray as $key=>$value)
            {
                if(substr($id,0,strlen($value)) == $value)
                { 
                    $num = substr($id,strlen($value));
                    $data[$channelKey][$num][$key] = ($key == 'time') ? $this->retime($span->nodeValue) : $span->nodeValue;
                }
            }
        }
        $numCount = count($data[$channelKey]);

        for($i=1; $i<=$numCount; $i++)

        {
            $data[$channelKey][$i]['time2'] = (isset($data[$channelKey][($i + 1)]['time'])) ? $data[$channelKey][($i + 1)]['time'] : '';    
            $data[$channelKey][$i]['channel'] = $channelNum . ' ' . $this->respacePlus($channelName);
        }

        return $data;
    }

    //Just return the Hi format for time and set the previous time 
    private function retime($timeAMPM)
    {
        $tmpDT = new DateTime(date('Y-m-d ') . $timeAMPM);
        $conv = $tmpDT->format('Hi');
        if($conv < $this->previousTime) $this->dt->add(new DateInterval('P1D'));
        $this->previousTime = $conv;
        return $this->dt->format('Ymd') . $conv;
    }
}
?>
Member Avatar for diafol

I gathered you were in a rush for this ?

I'm sorry but the code is not working, it show as empty screen. And your code is a bit in a mess. However, I have tidy up and clear the code so i can read it better.

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;

$xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '
<tv generator-info-name="www.mysite.com/xmltv">';

$baseUrl = file_get_contents('http://67.23.248.61/~ytestbox/get-listing.php');

$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
//@$domdoc->loadHTMLFile($baseUrl);
@$domdoc->loadHTML($baseUrl);

//$links = $domdoc->getElementsByTagName('test');
//$links = $domdoc->getElementById('test');
$links = $domdoc->getElementsByTagName('a');

$data = array();
foreach($links as $link)
{  
  //echo $domdoc->saveXML($link);
  if($link->getAttribute('href'))
  {
     if(!$link->hasAttribute('id') || $link->getAttribute('id')!='streams')
     {
       $url = str_replace("rtmp://", "", $link->getAttribute('href'));
       $url = str_replace(" ", "%20", $link->getAttribute('href'));
       //echo $url;
       //echo "<br>";
       $sdoc = new DOMDocument();
       $sdoc->strictErrorChecking = false;
       $sdoc->recover=true;
       @$sdoc->loadHTMLFile($url);
       $spans = $sdoc->getElementsByTagName('span');
       //echo $spans;
       $query = parse_url($url)['query'];
       $url_split = explode("&", $query)[0];
       $channel = urldecode(explode("=",$url_split)[1]);

       $flag=0;
       foreach($spans as $span)
       {
         echo $span->nodeValue;
     }
  }
}
?>

The code is working fine for me, I can read the code easily and I have got the input:

5:00 PMEragon7:30 PMHarry Potter and the Half-Blood Prince11:00 PMThe 700 Club12:00 AMThe Fresh Prince of Bel-Air - Same Game, Next Season12:30 AMThe Fresh Prince of Bel-Air - Three's a Crowd1:00 AMThe Fresh Prince of Bel-Air - It's a Wonderful Lie1:30 AMThe Fresh Prince of Bel-Air - Bullets Over Bel-Air2:00 AMSummer Sexy With T25!2:30 AMThe T-Fal OptiGrill - Grill like a Pro3:00 AMThe 700 Club4:00 AMDerm Exclusive!4:30 AMSleep Better!5:00 AMJoseph Prince5:30 AMLife Today With James Robison - Beth Moore - CN56:00 AMJoyce Meyer: Enjoying Everyday Life6:30 AMAmazing Facts Presents7:00 AMThat '70s Show - That '70s Finale7:30 AM

How do I get the time1, time2, time3, time4...etc into arrays to save the values and how I can input these time in start="" and end="" tag using the values?

example:

<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.testbox.elementfx.com/test">
  <channel id='101 ABC FAMILY'>
    <display-name>101 ABC FAMILY</display-name>
  <programme channel='101 ABC FAMILY' start='20140429170000' stop='20140429193000'>
    <title lang="en">Eragon</title>
    <sub-title lang="en"> </sub-title>
    <desc lang="en"> </desc>
    <category lang="en"> </category>
  </programme>
  <programme channel='101 ABC FAMILY' start='20140429193000' stop='20140429230000'>
    <title lang="en">Harry Potter and the Half-Blood Prince</title>
    <sub-title lang="en"> </sub-title>
    <desc lang="en"> </desc>
    <category lang="en"> </category>
  </programme>
  <programme channel='101 ABC FAMILY' start='20140429230000' stop='20140430000000'>
    <title lang="en">Harry Potter and the Half-Blood Prince</title>
    <sub-title lang="en"> </sub-title>
    <desc lang="en"> </desc>
    <category lang="en"> </category>
  </programme>
Member Avatar for diafol

I'm sorry but the code is not working, it show as empty screen.

Of course it's an empty screen - it's xml. It works perfectly for me.

And your code is a bit in a mess. However, I have tidy up and clear the code so i can read it better.

Really? OK, well done. However, by cutting out selected bits, you'll probably break it. I'm not looking at it again - I got it to work for you - if you can't get it to work, track down the error and let me know.

How do I get the time1, time2, time3, time4...etc into arrays to save the values and how I can input these time in start="" and end="" tag using the values?

My class did that. It output the values to the right place in the xml. I don't know what else you want it to do.

I have told you that I have got it working.

Here is the input:

5:00 PMEragon7:30 PMHarry Potter and the Half-Blood Prince11:00 PMThe 700 Club12:00 AMThe Fresh Prince of Bel-Air - Same Game, Next Season12:30 AMThe Fresh Prince of Bel-Air - Three's a Crowd1:00 AMThe Fresh Prince of Bel-Air - It's a Wonderful Lie1:30 AMThe Fresh Prince of Bel-Air - Bullets Over Bel-Air2:00 AMSummer Sexy With T25!2:30 AMThe T-Fal OptiGrill - Grill like a Pro3:00 AMThe 700 Club4:00 AMDerm Exclusive!4:30 AMSleep Better!5:00 AMJoseph Prince5:30 AMLife Today With James Robison - Beth Moore - CN56:00 AMJoyce Meyer: Enjoying Everyday Life6:30 AMAmazing Facts Presents7:00 AMThat '70s Show - That '70s Finale7:30 AM

Do you know how I can get the time because I'm using $sdoc->getElementsByTagName('span'); which it will get the time and programme title. So how I can only get the time and programme title seperate that I want to create two variables?

Member Avatar for diafol

look at my class it's all there.

Ok, I have almost done it now. I have got the list of format which it show in the output:

<programme channel='101 ABC FAMILY start='20140503110000' stop=''><programme channel='101 ABC FAMILY start='20140503133000' stop='20140503110000'><programme channel='101 ABC FAMILY start='20140503153000' stop='20140503133000'><programme channel='101 ABC FAMILY start='20140503175300' stop='20140503153000'><programme channel='101 ABC FAMILY start='20140503202000' stop='20140503175300'><programme channel='101 ABC FAMILY start='20140503223000' stop='20140503202000'><programme channel='101 ABC FAMILY start='20140504013000' stop='20140503223000'>

Here is the update code:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;

function getState($string)
{
  $ex = explode(" ",$string);
  return $ex[1];
}

$xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '
<tv generator-info-name="www.testbox.elementfx.com/xmltv">';
$baseUrl = file_get_contents('http://67.23.248.61/~ytestbox/get-listing.php');

$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
//@$domdoc->loadHTMLFile($baseUrl);
@$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementsByTagName('a');
$i = 0;
$count = 0;

$data = array();
foreach($links as $link)
{  
  //echo $domdoc->saveXML($link);
  if($link->getAttribute('href'))
  {
     if(!$link->hasAttribute('id') || $link->getAttribute('id')!='streams')
     {
       $url = str_replace("rtmp://", "", $link->getAttribute('href'));
       $url = str_replace(" ", "%20", $link->getAttribute('href'));
       //echo $url;
       //echo "<br>";
       $sdoc = new DOMDocument();
       $sdoc->strictErrorChecking = false;
       $sdoc->recover=true;
       @$sdoc->loadHTMLFile($url);
       //$time1_span = $sdoc->getElementById('time1');       
       //$spans = $sdoc->getElementsByTagName('time1');
       $query = parse_url($url)['query'];
       $channel_split = explode("&", $query)[0];
       $channel = urldecode(explode("=",$channel_split)[1]);
       $id_split = explode("&", $query)[1];
       $my_id = urldecode(explode("=",$id_split)[1]);
       $xpath = new DOMXpath($sdoc);
       //$time1 = $xpath->query("*/span[@id='time1']");
       //$time2 = $xpath->query("*/span[@id='time2']");
       //$time3 = $xpath->query("*/span[@id='time3']");
       //$time4 = $xpath->query("*/span[@id='time4']");       

       $time_arr = array();
       for ($i = 1; $i < 70; $i++)
       {
         $time_arr[] = $xpath->query("*/span[@id='time".$i."']");
       }

       // Save the output format
       $DATE_FORMAT_STRING = "YmdHis";

       // GET the current STAGE
       $current_state = getState($array[0]->nodeValue);
       $offset = 0;
       $flag = 0;

       foreach($time_arr as $time)
       {
         //echo $time->item(0)->nodeValue;

         // Get the item state.  
         //$this_state = getState($time);
         $this_state = getState($time->item(0)->nodeValue);
         //echo $time->nodeValue;

         // check if we past a day? 
         if($current_state == "PM" && $this_state == "AM")
         {
           $offset++;
         }
         $this_unix = strtotime($time->item(0)->nodeValue) + (60 * 60 * 24 * $offset);
         $values[] = date($DATE_FORMAT_STRING, $this_unix);
         //echo date($DATE_FORMAT_STRING, $this_unix);
         //echo $values[$count];
         //echo "<br></br>";

         $starttime = $values[$count];
         //echo $starttime;
         echo "<programme channel='".$my_id." ".$channel." start='".$starttime."' stop='".$stoptime."'>";

     if($flag>0)
     {
       echo "<programme channel='".$my_id." ".$channel." start='".$starttime."' stop='".$stoptime."'>";
       $stoptime = $starttime;
       $flag=1;
     }
     else
     {
       $stoptime = $starttime;
     } 
         $current_state = $this_state;
         $count++;
       }
     }
  }
}
?>

How do you input the format in the end=""?

example: I want to input the 20140503133000 in the end="" and start="", for the next format 20140503153000 I want to input them in the end="" and start="". The same things it goes for other format.

How I can do that using with my code?

Member Avatar for diafol

As I said my class does that. I'm not going to start deconstructing things. Have a look at my code - the answer is there. OK, this is my last post here - good luck with it, hope you get it solved to your satisfaction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.