Php Rss提要,生成图像
我正在尝试使用下面的代码在我的站点上生成RSS提要。rss正在出现,但我有两个问题:Php Rss提要,生成图像,php,rss,Php,Rss,我正在尝试使用下面的代码在我的站点上生成RSS提要。rss正在出现,但我有两个问题: 当feed显示在我的页面上时,图像不会显示,相反,你会看到img链接直接显示在页面上,如下所示 我如何限制在我的网站上出现的文章数量 以下是RSS的链接: 以下是我正在使用的代码: <?php $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; $locations = array('http://
<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$locations = array('http://topics.nytimes.com/topics/reference/timestopics/subjects/e/espionage/index.html?rss=1');
srand((float) microtime() * 10000000); // seed the random gen
$random_key = array_rand($locations);
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
printf("<dt><b><a href='%s' target=new>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description)));
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($locations[$random_key], 'r')
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>
在endElement()
中,当输出提要内容时,它调用printf(“%s
”),htmlspecialchars(trim($description))代码>
如果你删除htmlspecialchars
函数,那么它应该正确地显示图像和其他html,而不是转换你自己写的代码吗?我删除了你说的显示图像的内容,现在提要真的一团糟,所有内容都显示为单个文本块,没有图片,没有标题。如果添加printf(“”)
打印完$description后,应修复格式。您告诉我要删除该部分,请删除该部分。。之后是什么意思?我只是想让你在打印描述时删除htmlspecialchars的用法。有关完整代码,请参见粘贴。
<?php
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $numShown;
if ($name == "ITEM") {
$numShown++;
if ($numShown >= 5) {
return ;
}
printf ( "<dt><b><a href='%s' target=new>%s</a></b></dt>", trim ( $link ), htmlspecialchars ( trim ( $title ) ) );
printf ( "<dt>%s</dt><br><br>", trim ( $description ) );
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}