Search code examples
phprss

How to get content of a text file echoed?


I am trying to build a rss reader which is getting it's URL from a .txt file. How can I enter it to $rss->load(Here, I would like to echo a URL written in a text file); So that it will read the url from the .txt file?

I am absolutely not advanced in PHP but I also did not find any answer here. Thanks for help!

 <?php
    $rss = new DOMDocument();
    $rss->load('here I would like to echo a URL written in a text file');

    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            );
        array_push($feed, $item);
    }
    $limit = 5;
    for($x=0;$x<$limit;$x++) {
        $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
        $link = $feed[$x]['link'];
        $description = $feed[$x]['desc'];
        $date = date('d-m-y H:i &#187; ', strtotime($feed[$x]['date']));
        echo ''.$date.'';
        echo '<b><a href="'.$link.'" title="'.$title.'">'.$title.'</a></b>. ';

        echo ''.$description.'';
        echo '<hr>';
    }
?>

Solution

  • Just one URL, with no decoration? Use file_get_contents() to read the file and then trim() to cut off any unwanted line endings, if any:

    $url = trim(file_get_contents('/path/to/file'));
    $rss->load($url);
    

    Or just:

    $rss->load(trim(file_get_contents('/path/to/file')));