Home > front end >  Get next title from XML link in PHP
Get next title from XML link in PHP

Time:06-24

I'm parsing XML link. I'm getting the title of the broadcast now, but i can't to get the title of the next broadcast. If anyone can help me, I would be grateful,

  <?php
     $url = 'https://hls.airy.tv/airytv/33';
     $xml=simplexml_load_file("$url"); 
     $line = 0;
     $time = date( "YmdHi" );
     foreach($xml->xpath('programme[@channel="33"]') as $item) {
       $start = substr( (string)$item["start"], 0, -8);
       $end   = substr( (string)$item["stop"], 0, -8);
           if ($time > $start && $time < $end) {
               echo "Now : ".$item->title. "<br>";
               echo "Next : ".$item->title.[$line 1];
           }
       $line  ;
     }
  ?>

OUTPUT

Now : Let's_Go_Thailand_Koh_Lanta;_a_place_the_discover_Ep_021
Next : Let's_Go_Thailand_Koh_Lanta;_a_place_the_discover_Ep_021Array

XML

<programme start="20220623090024  0000" stop="20220623093300  0000" channel="33">
<title lang="en">Let's_Go_Thailand_Koh_Lanta;_a_place_the_discover_Ep_021</title>
<sub-title lang="en">Let's_Go_Thailand_Koh_Lanta;_a_place_the_discover_Ep_021</sub-title>
<desc lang="en"/>
<date>20220623</date>
<publisher/>
<category lang="en">Featured</category>
<category lang="en">Airy TV 1</category>
</programme>
<programme start="20220623093300  0000" stop="20220623100301  0000" channel="33">
<title lang="en">Let's_Go_Thailand_Moment_by_Moment_Harvest_Ep_023</title>
<sub-title lang="en">Let's_Go_Thailand_Moment_by_Moment_Harvest_Ep_023</sub-title>
<desc lang="en"/>
<date>20220623</date>
<publisher/>
<category lang="en">Featured</category>
<category lang="en">Airy TV 1</category>
</programme>

CodePudding user response:

Try this, it is working for me:

<?php
$url = 'https://hls.airy.tv/airytv/33';
$xml=simplexml_load_file("$url");
$time = date( "YmdHi" );

$currTitle = null;
$nextTitle = 'N/A';

foreach($xml->xpath('programme[@channel="33"]') as $item)
{
    if ($currTitle != null)
    {
        $nextTitle = $item->title;
        break;
    }

    $start = substr( (string)$item["start"], 0, -8);
    $end   = substr( (string)$item["stop"], 0, -8);
    if ($time > $start && $time < $end)
    {
        $currTitle = $item->title;
    }
}

echo "Now : $currTitle<br />Next : $nextTitle";
?>

I don't think you can do a lookahead like you're trying to do with the foreach, so you can just save off the the title when you find the record with the appropriate time, and let the loop progress one more time to pick up the next record.

  • Related