Site Tools


webseite:php:xmlreader

XML Reader/ RSS Feed

I wanted my own ad-free RSS Reader…

Code

I didnt want to load the .xml on access because I have this feed on the page which opens everytime I open a new tab in my browser.

Add to crontab

*/15 * * * * wget -O -q <YOURLOCALDESTINATION> '<URLOFXMLTODOWNLOAD>'

Add to webserver

<?php
//#### Read XML ####
$xml=simplexml_load_file("<YOURLOCALXML>") or die("Error: Cannot create object");
$news = $xml->xpath('//item');
 
//#### Create Table ####
echo '<table style="font-size:14px">';
 
//#### Foreach news article do ####
foreach($news as $a)
{
        //#### Title of news ####
        echo '<tr><td colspan="2"><a href="' . $a->link . '">' . $a->title . '</a></td></tr>';
        //#### Date of news ####
        echo '<tr><td colspan="2">' . $a->pubDate . '</td></tr>';
        //#### Picture in news ####
        echo '<tr><td><img src="' . $a->enclosure['url'] . '""width="120" height="120"></td>';
        //#### Remove HTML tags from description ####
        $desc = strip_tags($a->description);
        //#### Open function to limit text to 250 chars ####
        echo '<td>' . truncate($desc) . '</td></tr>';
        //#### Some empty rows ####
        echo '<tr><td colspan="2"><br/></td></tr>';
        echo '<tr><td colspan="2"><br/></td></tr>';
}
echo "</table>";
 
//#### Function to limit text to 250 chars ####
//#### https://stackoverflow.com/questions/3161816/get-first-n-characters-of-a-string ####
function truncate($string,$length=250,$append="&hellip;") {
  $string = trim($string);
 
  if(strlen($string) > $length) {
    $string = wordwrap($string, $length);
    $string = explode("\n", $string, 2);
    $string = $string[0] . $append;
  }
 
  return $string;
}
 
?>

Insert to your page for the feed

<iframe id="frame1" src="rssreader.php" width="400" height="350"></iframe>



webseite/php/xmlreader.txt · Last modified: 2020/12/21 20:05 by lunetikk