我正在分析XML链接.我现在得到了广播的标题,但我无法得到下一个广播的标题.如果有人能帮助我,我将不胜感激,

  <?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++;
     }
  ?>

输出

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>

推荐答案

试试这个,它对我有用:

<?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";
?>

我不认为您可以像使用foreach一样进行前瞻性操作,所以您可以在适当的时间找到记录时保存标题,然后让循环再进行一次以获取下一条记录.

Php相关问答推荐

向在添加到购物车上动态创建的WooCommerce产品添加发货类别

致命错误:未捕获错误:Google\Auth\HttpHandler\HttpHandlerFactory::build()

如何在php中生成包含第100秒的时间序列?

在WooCommerce中的本地收件发货方式下添加一个 Select 字段

添加并显示单价字段到WooCommerce产品变化

将SVG包含在另一个php生成的SVG中

使用与OpenSSL命令行兼容的AES-256-CTR为大型文件创建php代码OpenSSL加密

隐藏WooCommerce管理子菜单;主页;来自store 经理

PHP Laravel 迁移文件不创建关系

WooCommerce 相关产品(按 children 类别)作为排名数学主要类别的后备

产品页面自定义复选框可对 WooCommerce 购物车小计启用百分比折扣

根据所选付款方式更改 WooCommerce 提交结帐按钮文本

PHP - 计算非数值数组

根据WooCommerce中的产品重量更改简单产品的输入数量步值

如何更改或管理Carbon时区,使其不会来回移动时钟

将样式文件或脚本引入WordPress模板

如何在光速网络服务器中使用 apache ..htaccess

PHP获取根据辞职日期自动续订的合同终止日期

Laravel:调试配置不会改变

如何在没有模拟的情况下进行测试?