-1

我目前正在设计一个包含 20 个框的页面,其中每个框代表一个 RSS 提要,使用 simplepie 作为我的解析器。

目前,脚本从每个提要中调用第一张图像,丢弃其余图像并将该图像设置为框的背景图像。

有了 20 个提要,我想尽可能保持代码小而高效。虽然我了解 $feed01 的单独设置,但 $feed02 是一个必要的混乱邪恶,这是 simplepie 工作所必需的。

我无法理解的是为什么 imageurl() 返回一个空值。

谁能教育我为什么会这样?

非常感谢

<?php
require_once('isnotabear/php/autoloader.php');

$feed01 = new SimplePie();
$feed01 -> set_feed_url ('http://link/to/rss01');
$success = $feed01 -> init();
$feed01 -> handle_content_type();
$max = $feed01 -> get_item_quantity(); // Where do we end?

$feed02 = new SimplePie();
$feed02 -> set_feed_url ('http://link/to/rss02');
$success = $feed02 -> init();
$feed02 -> handle_content_type();
$max = $feed02 -> get_item_quantity(); // Where do we end?

$start = (isset($_GET['start']) && !empty($_GET['start'])) ? $_GET['start'] : 0; // Where do we start?
$length = (isset($_GET['length']) && !empty($_GET['length'])) ? $_GET['length'] : 1; // How many per page?


function imageurl()
{
    if ($success): foreach($feedin->get_items($start, $length) as $item): $feedin =     $item-> get_feed();
    {
        $replace = preg_match( '@src="([^"]+)"@' , $item->get_content(), $match );
        $match = preg_replace ('"src="', '', $match);
        $match = str_replace('"', "'", $match);
        echo $match[0];
    }
    endforeach;
    endif;
};

?>
<!DOCTYPE HTML>
... YADA YADA YADA ...

<a href="#" style="background-image:url(<?php $feedin = $feed01; imageurl(); ?>)"><div> <span>FEED NAME</span></div></a>
4

2 回答 2

1

$success内部没有定义imageurl(),所以它总是 null/false

你可能想要:

function imageurl() {
   global $success;
   if ($success) { .... }
}

但是不要像那样使用全局变量。将 $success 作为参数传入。会干净很多。

于 2013-03-13T18:38:45.333 回答
0

This is part-way between a comment and an answer, but I wanted the space to show the code. Your function uses a really awkward mix of PHP syntax and some entirely misleading punctuation:

  • the if(): ... endif; and foreach(): ... endforeach; syntax is extremely old-fashioned, and generally used only when using PHP as a template language (i.e. in the middle of HTML) rather than in a standalone function like this
  • the {, }, and indenting relate to neither the if nor the foreach; PHP will ignore them, but they'll confuse anyone reading your code
  • the ; at the end is also ignored - a function ends at its }

So your code should actually look like this (note I've also added $success and $feedin as parameters which you should pass in to the function):

function imageurl($success, $feedin)
{
    if ($success)
    {
        foreach($feedin->get_items($start, $length) as $item)
        {
            $feedin = $item->get_feed();
            $replace = preg_match( '@src="([^"]+)"@' , $item->get_content(), $match );
            $match = preg_replace ('"src="', '', $match);
            $match = str_replace('"', "'", $match);
            echo $match[0];
        }
    }
}

Oh, and you probably want to return $match[0] rather than echo it. Otherwise, it's still going to return null whatever you pass in.

于 2013-03-13T19:15:03.577 回答