Created
January 10, 2021 15:53
-
-
Save osteel/890301065b78985f439722f6f1956010 to your computer and use it in GitHub Desktop.
Example PHP script to parse a blog's RSS feed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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