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!


Comments
RSS Comments Feed
Matt
I recommend updating.
Patric
Fred K
Now, if we only had a "summary" or "snippet" to tie this in with we could do something with the category index page as well... lol (look for post on forum about that little nugget in a little while...)
Matt
Cool, I can't wait to see what you do.
I've had so many ideas regarding that... i.e., a special summary field for viewing on the index pages, and a summary thumbnail image, etc.
--Matt
asundrus
slemborg
I'm trying to strip my rss feed for the bbcodes I made together with asundrus, but, let's say I wanna strip [i ]text[/i ] I then found a solution, but it aint the best, as I have to add each bbcode, like:
for [i ] I made '/\[i\]/'
and for [/i ] I made '/\[\/i\]/'
I search around trying to find a simple solution, but maybe im just looking the wrong places.
Any thoughts?
ps. spaces added in bbcodes incase you article picks up the code.
Matt
preg_replace("/(\[.*\])(.*)(\[\/.*\])/e","'\\2'",$text);slemborg
Different
What can you advise to utf8?
Matt
if (strlen(utf8_decode($output)) > 400) {Different
Thanks for your reply, but it did not help.
Could be an error occurs even after counting characters -> in subst function...
Different
// MAKE SUBSTR FUNCTION FOR UTF-8 function utf8_substr($str,$from,$len){ return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'. '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s', '$1',$str); }I replaced a couple of lines in the mod:
if (strlen(utf8_decode($output)) > 400) { $output = utf8_substr($output,0,400);Different
Sorry for the frequent comments. The final version looks like this:
if (strlen(utf8_decode($output)) > 400) { $output = utf8_substr($output,0,400); $pos = strrpos(utf8_decode($output), '. '); if ($pos !== false && $pos > 300) { // there's a sentence end close by, let's cut it there $output = utf8_substr($output,0,$pos).'.';Matt
mb_strlen($output, 'UTF-8')
and then again with mb_substr?
After looking at it for a minute here, the easiest thing to do should be to use utf8_decode on the output only once, then do everything against that value.
$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>')); <span class="highlight">$output = utf8_decode($output);</span> if (strlen($output) > 400) {Different
*LOL* I forgot to add php_mbstring.dll for my local server...
Now everything works as expected! See below:
if (mb_strlen($output,'UTF-8') > 400) { $output = mb_substr($output,0,400,'UTF-8'); $pos = mb_strrpos($output, '. ','UTF-8'); if ($pos !== false && $pos > 300) { $output = mb_substr($output,0,$pos,'UTF-8').'.';The same should be done for comments:
toolman
How do I add a category of the article to an RSS feed?
I can not find a solution to this problem.
Would you be kind to see what we can do about it?
----------------------------------------
Translation from Polish: translate.google.pl
Matt
SvenPhilippe
You know what Matt?
I think one thing is missing: a read on link to a #jump anchor (remember http://snewscms.com/forum/index.php?topic=8177)
Matt
SvenPhilippe
I'll be waiting.
Matt
Inside the function rss_contents, find the following and add the bits I've added (bolded);
That should add a "read more" link to rss-articles RSS feeds.
SvenPhilippe
SvenPhilippe
I don't know if you have your feed published in other sites but there might be a strong SEO improvement:
The idea is to replaced in the feed break the read more text anchor created by the one from "An easy mod to create custom break titles for your sNews articles".
What do you think of it?
Matt
if ($rss_item == "rss-articles") { if (preg_match("/\[break title=\"(.*)\"\]/i",$r['text'],$matches)) { $readmore = $matches[1]; } else { $readmore = l('read_more'); } $jump = '<br /><a href="'.$link.'#jump">'.$readmore.'</a>'; }SvenPhilippe
Cutting the text works but if it's in a link there's no more link at all.
I tried to fix by cutting the text without words breaking but as you know PHP is not my cup of tea (anyway I don't care, I only drink alcohol, even for breakfast) so I didn't get any results using this:
That nice piece of code was found there: http://www.php.net/manual/en/function.substr.php