1

我正在尝试从外部文件中获取变量,但我需要先执行该文件。我怎样才能做到这一点?

我需要获取变量:$title, $description, $link, $date, 和$author.

rss.php:

$rss2_file = 'http://www.rune-pk.org/forums/external.php?forumids=2&type=rss2';


$is_item = false;
$tag = '';
$title = '';
$description = '';
$link = '';
$date = '';
$author = '';

function character_data($parser, $data) {
    global $is_item, $tag, $title, $description, $link, $date, $author;
    if ($is_item) {
        switch ($tag) {
            case "TITLE":
                $title .= $data;
                break;

            case "DESCRIPTION":
                $description .= $data;
                break;

            case "LINK":
                $link .= $data;
                break;

            case "PUBDATE":
                $date .= $data;
                break;

            case "AUTHOR":
                $author .= $data;
                break;
        }
    }
}

function begin_element($parser, $name) {
    global $is_item, $tag;
    if ($is_item) {
        $tag = $name;
    } else if ($name == "ITEM") {
        $is_item = true;
    }
}

function end_element($parser, $name) {
    global $is_item, $title, $description, $link, $date, $author, $rss2_output;
    if ($name == "ITEM") {
        $rss2_output .= "<dt><strong><a href='" . trim($link) . "'>" . htmlspecialchars(trim($title)) . "</a></strong> - " . htmlspecialchars(trim($date)) . " by <em>" . htmlspecialchars(trim($author)) . "</em></dt><dd>" . htmlspecialchars(trim($description)) . "</dd>";
        $title = "";
        $description = "";
        $link = "";
        $date = "";
        $author = "";
        $is_item = false;
    }
}

$parser = xml_parser_create();

xml_set_element_handler($parser, "begin_element", "end_element");
xml_set_character_data_handler($parser, "character_data");
$fp = fopen($rss2_file, "r");

while ($data = fread($fp, 4096)) {
    xml_parse($parser, $data, feof($fp));
}

fclose($fp);

xml_parser_free($parser);

echo "Latest Announcements:";
echo $rss2_output;
?>

索引.php:

include 'rss.php';

echo $rss->title;

?>
4

1 回答 1

5

您尝试的是用于访问存储在类中的变量。但这不是这里的情况,所以这样做:

<?php
    include 'rss.php';
    echo $title;
?>
于 2013-01-01T23:14:46.253 回答