外部文件执行后如何获取外部变量?

时间:2013-01-01 23:11:24

标签: php variables

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

我需要获取变量:$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;
?>

的index.php:     

include 'rss.php';

echo $rss->title;

?>

1 个答案:

答案 0 :(得分:5)

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

<?php
    include 'rss.php';
    echo $title;
?>