简体   繁体   English

如何使用php从外部XML中排除某些内容?

[英]How to exclude certain content from external XML with php?

Continuing this theme How to display remote XML file with Wordpress in frontend using php? 继续这个主题如何使用php在前端使用Wordpress显示远程XML文件? i was asked the following issue: 我被问到以下问题:

<?php
$xmlhd = wp_remote_get('http://www.myurl.com/api/channel.php?type=hd');
$xmlparseado = simplexml_load_string($xmlhd['body']);

$content = '';
echo "<ul>";
$rows = $xmlparseado->channel->row;
foreach($rows as $key=>$row){   
    if($key =='row'){
        $row_string = '<li>';
        $row_string.= '<span>'.$row->date.'</span>';
        $row_string.= '<span>'.$row->time.'</span>';
        $row_string.= '<span>'.$row->description.'</span>';
        $row_string.= '<span>'.$row->imagethumb.'</span>';
        $row_string.= '</li>';
        $content.=$row_string;
    }   
}
echo $content;
echo "</ul>";
?>

The XML returns: XML返回:

<programations>
    <channel name="KCBS HD">
        <row>
            <date>july, 23</date>
            <time>06:00</time>
            <title><![CDATA[ WKCBS Action News ]]></title>
            <description><![CDATA[ Action News, hosted by: Jenn Doe ]]></description>
            <imagethumb/>
        </row>
        <row>
            <date>July, 23</date>
            <time>06:35</time>
            <title><![CDATA[ KCBS Sports Center ]]></title>
            <description><![CDATA[ The best scoreS from the Sportscenter stadium, hosted by: Fernando Sobalaprieta ]]></description>
            <imagethumb/>
        </row>
    </channel>
</programations>

This code displays a list that consists of the following: 此代码显示包含以下内容的列表:

  • date 日期
  • time 时间
  • description 描述
  • image 图片

There are many, but I was asked to show the date, only the first entry, ie: 有很多,但是我被要求显示日期,只有第一个条目,即:

First entry: 第一次进入:

  • date 日期
  • time 时间
  • description 描述
  • image 图片

Second entry and more: 第二次入境及更多:

  • time 时间
  • description 描述
  • image 图片

The problem is that when you get the end of the day, the date changes to the next day so I can not use conditional. 问题在于,当您结束一天时, 日期更改为第二天,因此我不能使用条件式。

Any suggestions? 有什么建议么?

Edit: 编辑:

Please remember this: 请记住这一点:

Every days, the first program display the date. 每隔一天,第一个程序都会显示日期。 :) :)

If I understood correctly, you want just the first entry to have the date displayed. 如果我理解正确,则只希望第一个条目显示日期。 So use a dummy counter: 因此,请使用虚拟计数器:

$count = 0;
$first_date = "";
foreach($rows as $key=>$row){   
if($key =='row'){
    $row_string = '<li>';
    if($count == 0 ){ 
    $first_date = $row->date; 
    $row_string.= '<span>'.$row->date.'</span>';
    }
    $row_string.= '<span>'.$row->time.'</span>';
    $row_string.= '<span>'.$row->description.'</span>';
    $row_string.= '<span>'.$row->imagethumb.'</span>';
    $row_string.= '</li>';
    $content.=$row_string;
    if( $count == 0 || strcmp( $row->date, $first_date ) == 0 ) 
    $count++;
    else $count = 0;
}   
}

Edit: okay, so now it will display the date only for the first appearing entry of that day. 编辑:好的,所以现在它将仅显示该天的第一个出现条目的日期。

Hm, try commenting out lines that you don't need. 嗯,尝试注释掉不需要的行。 Btw, I hope I got this right. 顺便说一句,我希望我做对了。 Try this for example: 尝试以下示例:

 $row_string = '<li>';
 $row_string.= '<span>'.$row->date.'</span>';
 //$row_string.= '<span>'.$row->time.'</span>';
 //$row_string.= '<span>'.$row->description.'</span>';
 $row_string.= '<span>'.$row->imagethumb.'</span>';
 $row_string.= '</li>';
 $content.=$row_string;

With // php will ignore those lines, like here you won't have time and description displayed no more. 使用// php将忽略这些行,例如,这里您将不再有时间并且不再显示说明。

Store the day in a separate variable, and when it changes, echo it out then. 将日期存储在一个单独的变量中,并在其发生变化时将其回显。

$dateHolder = "";
foreach($rows as $key=>$row){   
    if($key =='row'){         
        $row_string = '<li>';
        if($dateHolder=="" || $dateHolder != $row->date) {
            $row_string.= '<span>'.$row->date.'</span>';
            $dateHolder = $row->date;
        }
        $row_string.= '<span>'.$row->time.'</span>';
        $row_string.= '<span>'.$row->description.'</span>';
        $row_string.= '<span>'.$row->imagethumb.'</span>';
        $row_string.= '</li>';
        $content.=$row_string;
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM