Skip to content

Instantly share code, notes, and snippets.

@osteel
Created January 10, 2021 15:53
Show Gist options
  • Save osteel/890301065b78985f439722f6f1956010 to your computer and use it in GitHub Desktop.
Save osteel/890301065b78985f439722f6f1956010 to your computer and use it in GitHub Desktop.
Example PHP script to parse a blog's RSS feed
<?php
// Load Composer's autoload
require_once __DIR__ . '/vendor/autoload.php';
// Load the RSS feed
$feed = Feed::loadRss('https://tech.osteel.me/feeds/rss.xml')->toArray();
// Generate the list of blog posts
$posts = '';
foreach (array_slice($feed['item'], 0, 5) as $post) {
$date = date('d/m/Y', strtotime($post['pubDate']));
$posts .= sprintf("\n* **[%s]** [%s](%s \"%s\")", $date, $post['title'], $post['link'], $post['title']);
}
// Generate the new content
$content = preg_replace(
'#<!-- posts -->.*<!-- /posts -->#s',
sprintf('<!-- posts -->%s<!-- /posts -->', $posts),
file_get_contents('README.md')
);
// Overwrite the file
file_put_contents('README.md', $content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment