如何在RSS提要为空时显示文本
How to display text when RSS feed is empty
我已经为此挣扎了一段时间了,我从一个类似的问题中得到了一些帮助,但我似乎不能让它在我的例子中工作。
我在我的网站上有一个RSS提要,我要做的是在RSS提要为空白/空时显示一条消息说"没有警告"。如果RSS提要中有内容,那么我只想显示它。
无论如何我都不能让这个工作!我无法让它识别feed是空白的…这可能吗?
这是我的代码,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Loading directly an RSS feed and displaying it</title></head>
<link type="text/css" href="rss-style.css" rel="stylesheet">
<body bgcolor="#FFE991">
<div id="zone" ><img src="/images/alert-icon-120.png" alt="Weather Warnings for SA. Issued by the Australian Bureau of Meteorology" align="left" style="padding-right: 4pt"width="15" height="15" border="0"/> Current South Australian Statewide Warnings:<small><em><font color="black"> Provided by The Bureau of Meteorology</font></em></small>
</div>
<fieldset class="rsslibbomsa">
<?php
require_once("rsslib.php");
$url = "http://www.bom.gov.au/fwo/IDZ00057.warnings_sa.xml";
$rss = RSS_Display($url, 15, false, false);
if ($rss == '')
{
// nothing shown, do whatever you want
echo 'No Current Warnings';
}
else
{
// something to display
echo $rss123;
}
?>
</fieldset>
</body>
</html>
如果有帮助的话,RSS_Display来自下面提供的rsslib.php文件,
<?php
/*
RSS Extractor and Displayer
(c) 2007-2010 Scriptol.com - Licence Mozilla 1.1.
rsslib.php
Requirements:
- PHP 5.
- A RSS feed.
Using the library:
Insert this code into the page that displays the RSS feed:
<?php
require_once("rsslib.php");
echo RSS_Display("http://www.bom.gov.au/fwo/IDZ00059.warnings_vic.xml", 15);
? >
*/
$RSS_Content = array();
function RSS_Tags($item, $type)
{
$y = array();
$tnl = $item->getElementsByTagName("title");
$tnl = $tnl->item(0);
$title = $tnl->firstChild->textContent;
$tnl = $item->getElementsByTagName("link");
$tnl = $tnl->item(0);
$link = $tnl->firstChild->textContent;
$tnl = $item->getElementsByTagName("pubDate");
$tnl = $tnl->item(0);
$date = $tnl->firstChild->textContent;
$tnl = $item->getElementsByTagName("description");
$tnl = $tnl->item(0);
$description = $tnl->firstChild->textContent;
$y["title"] = $title;
$y["link"] = $link;
$y["date"] = $date;
$y["description"] = $description;
$y["type"] = $type;
return $y;
}
function RSS_Channel($channel)
{
global $RSS_Content;
$items = $channel->getElementsByTagName("item");
// Processing channel
$y = RSS_Tags($channel, 0); // get description of channel, type 0
array_push($RSS_Content, $y);
// Processing articles
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
function RSS_Retrieve($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
RSS_Channel($channel);
}
}
function RSS_RetrieveLinks($url)
{
global $RSS_Content;
$doc = new DOMDocument();
$doc->load($url);
$channels = $doc->getElementsByTagName("channel");
$RSS_Content = array();
foreach($channels as $channel)
{
$items = $channel->getElementsByTagName("item");
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}
}
}
function RSS_Links($url, $size = 15)
{
global $RSS_Content;
$page = "<ul>";
RSS_RetrieveLinks($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size + 1);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0) continue;
$title = $article["title"];
$link = $article["link"];
$page .= "<li><a href='"$link'">$title</a></li>'n";
}
$page .="</ul>/n";
return $page;
}
function RSS_Display($url, $size = 15, $site = 0, $withdate = 0)
{
global $RSS_Content;
$opened = false;
$page = "";
$site = (intval($site) == 0) ? 1 : 0;
RSS_Retrieve($url);
if($size > 0)
$recents = array_slice($RSS_Content, $site, $size + 1 - $site);
foreach($recents as $article)
{
$type = $article["type"];
if($type == 0)
{
if($opened == true)
{
$page .="</ul>'n";
$opened = false;
}
$page .="<b>";
}
else
{
if($opened == false)
{
$page .= "<ul>'n";
$opened = true;
}
}
$title = $article["title"];
$link = $article["link"];
$page .= "<li><a href='"$link'">$title</a>";
if($withdate)
{
$date = $article["date"];
$page .=' <span class="rssdate">'.$date.'</span>';
}
$description = $article["description"];
if($description != false)
{
$page .= "<br><span class='rssdesc'>$description</span>";
}
$page .= "</li>'n";
if($type==0)
{
$page .="</b><br />";
}
}
if($opened == true)
{
$page .="</ul>'n";
}
return $page."'n";
}
?>
任何帮助都将非常感激。
许多谢谢。查看rsslib代码,可以看到如果rss没有文章,则返回的字符串将是'n
。
检查RSS是否为空的测试应该是if ($rss == "'n")
除此之外,rsslib似乎是一个旧代码(2010),它不是面向对象的(这是一个问题,因为有全局变量定义的函数之外,可以在与您的代码冲突。
此外,这个类完成RSS的所有格式化,因此如果您想更改它,则需要更改rsslib代码。
你应该考虑使用像这样的其他类:https://github.com/dg/rss-php,它是面向对象的,易于使用。
相关文章:
- xmlhttp.responseText不显示文本
- 如何在MVC中使用jQuery在文本框旁边显示文本
- 根据页面的位置突出显示文本中的字符
- 当选择值x时,Javascript需要在正确的位置显示文本区域,从而循环通过具有选择选项的表单
- 操作javascript变量[HTML]以只显示文本
- 如何显示文本长度,即使它超过ng最大长度
- 如何在悬停时显示文本而不移动页面上的内容
- ASP.NET MVC,在文本框旁边显示文本
- 可单击滚动图像以显示文本框
- 使用鼠标悬停JavaScript/HTML显示文本
- 延迟高亮显示文本区域中的文本
- 选择“选择选项”时显示文本.怎么做
- 无法使用JavaScript在画布上显示文本
- 在不更改HTML源代码的情况下,在管理员TinyMCE编辑器中突出显示文本
- 将鼠标悬停在图像上时显示文本
- javascript:在for循环中使用settimeout来定期显示文本
- 一页主题搜索/用下一个按钮突出显示文本
- JavaScript 在更改选择时不显示文本区域
- 用于突出显示文本的JQuery/JavaScript插件
- 模式显示文本区域内容不是字符串