过滤 RSS 源以显示今天的内容并按日期时间降序排序



我有一个小脚本(在网上找到(,它将RSS提要收集到一个数组中,对数组进行排序,然后显示您想要的方式。现在,我希望只能显示特定日期(仅限今天(提要中的项目。任何人都可以帮助过滤排序的数组吗?

<?php
//Feed URLs
$feeds = array(
"https://www.abda.de/?type=1102",
"https://www.abda.de/?type=1100",
);
//Read each feed's items
$entries = array();
foreach($feeds as $feed) {
$xml = simplexml_load_file($feed);
$entries = array_merge($entries, $xml->xpath("//item"));
}
//Sort feed entries by pubDate
usort($entries, function ($feed1, $feed2) {
return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
});
function showrssitem($i) {
global $entries;
$backgroundbutton = 'd3d3d3';
switch ($entries[$i]->category) {
case "Chargenrückrufe" :
$backgroundbutton = 'ff9933';       /* orange */
break;
case "Rückrufe" :
$backgroundbutton = 'ff9933';       /* orange */
break;
case "Informationen der Institutionen und Behörden" : 
$backgroundbutton = 'ff3300';       /* red */
break;
case "Informationen der Hersteller" : 
$backgroundbutton = 'ff5500';       /* red 2 */
break;
case "AMK-PhaGro-Schnellinformationen" : 
$backgroundbutton = 'ff7700';       /* red3  */
break;
}
$strippedentry1 = str_replace ("Online-Nachricht: ","", $entries[$i]->title); 
$strippedentry2 = str_replace ("Informationen der Institutionen und Behörden: ","", $strippedentry1); 
$strippedentry3 = str_replace ("Informationen der Hersteller: ","", $strippedentry2); 
echo '<a style="text-decoration: none;" target="_blank" href="' . $entries[$i]->link . '"><button style="background-color: #' . $backgroundbutton . '; margin-bottom: 10px; width: 100%; height:75px; display: block; border-radius: 10px;">' . $entries[$i]->category . '</button></a>';
echo mb_strimwidth($strippedentry3,0,100,"...") . "<br>";
echo "<i>[" . strftime('%d/%m/%Y', strtotime($entries[$i]->pubDate)) . "]</i><br>";
}
?>

以下解决方案现在对我有用。 如果您对更清洁的方式有任何建议,请随时发表评论;)

<?php
//Feed URLs
$feeds = array(
"https://www.abda.de/?type=1102",
"https://www.abda.de/?type=1100",
);
//Read each feed's items
$entries = array();
foreach($feeds as $feed) {
$xml = simplexml_load_file($feed);
$entries = array_merge($entries, ($xml->xpath("//item")));
}
//Sort feed entries by pubDate
usort($entries, function ($feed1, $feed2) {
return strtotime($feed2->pubDate) - strtotime($feed1->pubDate);
});
//Find latest date in array         $max = max(array_map('strtotime', $arr));
$highestdate_entries = date('d-m-Y', max(array_map('strtotime', $entries)));    /* returns format 02-06-2020 */
$entries_filtered = array_filter($entries, function ($var) use ($highestdate_entries) {
return date('d-m-Y',(strtotime($var->pubDate))) === $highestdate_entries;       /* -> returns 1 */
});
//Reset the array so eg. id 10 moves to id 0
$entries_filtered = array_values($entries_filtered);
//Count the amount of items, so the for loop can be adjusted to that
$numberofitems_entries = count($entries);
$numberofitems_entries_filtered = count($entries_filtered);
//echo "Items in entries : " . $numberofitems_entries . "<br>";
//echo "Items in entries_filtered : " . $numberofitems_entries_filtered . "<br>" ;
//echo "Latest date in entries : " . $highestdate_entries . "<br>" ;
//echo strtotime($entries_filtered[0]->pubDate) . " === " . strtotime($dateoftoday);
// XML feed provides 50 items per feed. Total is 100 items.
// $dateoffeed=strftime("%d-%m-%y",strtotime($entries[0]->pubDate));
// echo $dateoftoday;
// Date format in xml feed:     Thu, 28 May 2020 12:17:38 +0200
// Date format from date:       Mon, 01 Jun 2020 10:13:48 +0200
// $dateoftoday=date(DATE_RFC2822);
function showrssitem($i) {
global $entries_filtered;
$backgroundbutton = 'd3d3d3';
switch ($entries_filtered[$i]->category) {
case "Chargenrückrufe" :
$backgroundbutton = 'ff9933';       /* orange */
break;
case "Rückrufe" :
$backgroundbutton = 'ff9933';       /* orange */
break;
case "Informationen der Institutionen und Behörden" : 
$backgroundbutton = 'ff3300';       /* red */
break;
case "Informationen der Hersteller" : 
$backgroundbutton = 'ff5500';       /* red 2 */
break;
case "AMK-PhaGro-Schnellinformationen" : 
$backgroundbutton = 'ff7700';       /* red3  */
break;
}
$strippedentry1 = str_replace ("Online-Nachricht: ","", $entries_filtered[$i]->title); 
$strippedentry2 = str_replace ("Informationen der Institutionen und Behörden: ","", $strippedentry1); 
$strippedentry3 = str_replace ("Informationen der Hersteller: ","", $strippedentry2); 
echo '<td style = "border=0px !important; width:20%"><a style="text-decoration: none;" target="_blank" href="' . $entries_filtered[$i]->link . '"><button style="background-color: #' . $backgroundbutton . '; margin-bottom: 0px; width: 100%; height:50px; display: block; border-radius: 10px;">' . $entries_filtered[$i]->category . '</button></a></td>';
echo "<td><i>" . strftime('%d/%m/%Y', strtotime($entries_filtered[$i]->pubDate)) . ":</i>&nbsp;&nbsp;&nbsp;";
echo mb_strimwidth($strippedentry3,0,200,"...") . '</td>';
}
?>

我建议将数据准备和数据呈现分开。

在一个地方执行所有筛选、排序和字符串操作。

以下所有内容都是未经测试的代码,可能包含错误。

define(
'CATEGORY_COLORS',
[
'Chargenrückrufe' => '#ff9933', // orange
'Rückrufe' => '#ff9933', // orange
'Informationen der Institutionen und Behörden' => '#ff3300', // red
'Informationen der Hersteller' => '#ff5500', // red 2
'AMK-PhaGro-Schnellinformationen' => '#ff7700', // red3
]
);
$feeds = [
"https://www.example.com/?type=1102",
"https://www.example.com/?type=1100",
];
$today = date('Y-m-d');
$datetimes = [];
$todaysEntries = [];
foreach ($feeds as $feed) {
foreach (simplexml_load_file($feed)->xpath("//item") as $entry) {
$dt = DateTime::createFromFormat('D, d F Y H:i:s O', $entry->pubDate); // Thu, 28 May 2020 12:17:38 +0200
if ($dt->format('Y-m-d') === $today) {
$datetimes[] = $dt->format('Y-m-d H:i:s');
$todaysEntries[] = (object) [
'category' => $entry->category,
'pubDate' => $dt->format('d/m/Y'),
'link' => $entry->link,
'color' => CATEGORY_COLORS[$entry->pubDate] ?? '#888888',
'title' => str_replace(
[
'Online-Nachricht: ',
'Informationen der Institutionen und Behörden: ',
'Informationen der Hersteller: ',
],
'',
$entry->title
)
];
}
}
}
array_multisort($datetimes, SORT_DESC, $todaysEntries);

然后只需打印您的 html 行即可。 (尝试将尽可能多的样式声明移动到外部样式表中。

foreach ($todaysEntries as $entry) { 
printf(
'<tr>
<td>
<a target="_blank" href="%s">
<button style="background-color: %s;">%s</button>
</a>
</td>
<td>
<i>%s</i>
&nbsp;&nbsp;&nbsp;
%s
</td>
</tr>',
$entry->link,
$entry->color,
$entry->category,
$entry->pubDate,
mb_strimwidth($entry->title, 0, 200, "...")
);
}

最新更新