RSS Feeds are broken in sNews, fix it with this hack

Wow, OK, shame on me for not realizing this before, maybe I should subscribe to my own feeds, but I was just made aware that my entire articles were being displayed in the RSS feed.

This is not how the feeds are generally intended to work, they should contain a linked title and a snippet of the article, I like 300 or 400 characters.

OK, so how to fix it? Easy, BACK-UP your snews.php and work off of a copy, now find function strip, and insert the highlighted text;

// PREPARING ARTICLE FOR XML
function strip($text) {
    $search = array('/\[include\](.*?)\[\/include\]/', '/\[func\](.*?)\[\/func\]/', '/\[break\]/', '/</', '/>/');
    $replace = array('', '', '', '<', '>');
    $output = preg_replace($search, $replace, $text);
    $output = stripslashes(strip_tags($output, '<a><img><h1><h2><h3><h4><h5><ul><li><ol><p><hr><br><b><i><strong><em><blockquote>'));
    if (strlen($output) > 400) { 
        $output = substr($output,0,400);
        $pos = strrpos($output, ". ");
        if ($pos !== false && $pos > 300) { // there's a sentence end close by, let's cut it there
            $output = substr($output,0,$pos).'.';
        } else {
            $output = $output.'...';
        }
    }
    return $output;
}

You may also want to strip all of the tags from the preview, to avoid unmatched tags, etc, i.e. change;

$output = stripslashes(strip_tags($output, '<a><img><h1><h2><h3><h4><h5><ul><li><ol><p><hr><br><b><i><strong><em><blockquote>'));

to

$output = stripslashes(strip_tags($output));

That's it, that will cut the text preview down to 300 characters max, and add a ... after if a word gets chopped. Thanks to Poppoll for the head's up on this one!

Share this post:
Share of Facebook Tweet this post Share this post on Google+ Email this post


Comments

RSS Comments Feed




(optional, not publicly displayed) (optional)

Site News

About Matt

Privacy Policy | About Me
Copyright © 2002 - 2012 Matt Jones
HTML5/CSS3
Back to Top Back to top