jarvis Posted October 28, 2010 Share Posted October 28, 2010 Hi, I've been passed the below code which converts rss to php and then displays HTML. It works well. However, I've 2 issues with it: 1) I'd like to set it so only say 5 posts are shown 2) How can I truncate the description, so it only shows say 100 chars or 20 words? Any help is much appreciated. Here's the code: <?php // DO NOT EDIT BELOW THIS LINE date_default_timezone_set("GMT"); $itemArray = array(); if($text = preg_replace("/[\t\r\n]/", " ", htmlentities(@file_get_contents($rss_feed_url), ENT_QUOTES))) { while(strpos($text, "â") == true) { $text = substr($text, 0, strpos($text, "â")) ."'". substr($text, strpos($text, "â")+9, strlen($text)); } $items = explode("<item>", html_entity_decode($text)); foreach($items as $item) { $title = ""; $link = ""; $desc = ""; $pubdate = ""; $image = ""; $item = html_entity_decode($item, ENT_QUOTES); $item = preg_replace('/&(?!amp;|quot;|nbsp;|gt;|lt;|laquo;|raquo;|copy;|reg;|bul;|rsquo;|mdash;|#)/', '&', $item); $item = str_replace("<![CDATA[","", $item); $item = str_replace("]]>","", $item); if(stripos($item, "</item>") == true) { if(preg_match('|<title>(.*)</title>|', $item, $match)) { $title = $match[1]; } if(preg_match('|<pubDate>(.*)</pubDate>|', $item, $match)) { $pubdate = strtotime($match[1]); } if(preg_match('|url="(.*)|', $item, $match)) { $search = $match[1]; $imageArray = explode('"', $search); $image = $imageArray[0]; } if(preg_match('|<description>(.*)</description>|', $item, $match)) { $desc = $match[1]; $desc = strip_tags($desc); } if(preg_match('|<link>(.*)</link>|', $item, $match)) { $link = $match[1]; $link = end(explode("*", $link)); } if($title && $link && $desc) { array_push($itemArray, "$pubdate|$title|$link|$desc|$image|"); } } } } else { print "<p>Whoops! We could not connect to: <a href=\"$rss_feed_url\">$rss_feed_url</a></p>"; } if(sort($itemArray)) { foreach(array_reverse($itemArray) as $iA) { list($pubdate,$title,$link,$desc,$image) = explode("|", $iA); $timenow = time(); $difference = $timenow-$pubdate; $days = 0; $hours = 0; $minutes = 0; print "<p>"; if($hotlink_images && $image) { if($open_links_in_new_window) { print "<a class=\"thumb\" href=\"$link\" onclick=\"openInNewWindow('$link'); return false\">" . "<img src=\"$image\" alt=\"$title\"></a>"; } else { print "<a class=\"thumb\" href=\"$link\"><img src=\"$image\" alt=\"$title\"></a>"; } } if($open_links_in_new_window) { print "<a href=\"$link\" onclick=\"openInNewWindow('$link'); return false\">$title</a><br>"; } else { print "<a href=\"$link\">$title</a><br>"; } if($pubdate) { print "<span class=\"sub\">Posted "; if(sprintf("%01.0d", $difference/(60*60*24))) { $days = sprintf("%01.0d", $difference/(60*60*24)); $difference -= $days*(60*60*24); if($days > 1) { print "$days days "; } else { print "$days day "; } } if(!(sprintf("%01.0d", $difference/(60*60*24))) && sprintf("%01.0d", $difference/(60*60))) { $hours = sprintf("%01.0d", $difference/(60*60)); $difference -= $hours*(60*60); if($hours > 1) { print "$hours hours "; } else { print "$hours hour "; } } if(!(sprintf("%01.0d", $difference/(60*60*24))) && sprintf("%01.0d", $difference/60)) { $minutes = sprintf("%01.0d", $difference/60); if($minutes > 1) { print "$minutes minutes "; } else { print "$minutes minute "; } } print "ago</span><br>"; } print "$desc</p>\n<div class=\"hr\"> </div>\n"; } } ?> Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/217089-rss-to-php-limit-description-no-posts/ Share on other sites More sharing options...
salathe Posted October 28, 2010 Share Posted October 28, 2010 Is there any reason why you're choosing to use regular expressions to access the XML content rather than one of the numerous dedicated tools built in to PHP for working with XML? It would make life easier and make your code more reliable and easier to work with/understand. Let us know if you're up for that approach, or would just rather us help you to fix up what you have. Quote Link to comment https://forums.phpfreaks.com/topic/217089-rss-to-php-limit-description-no-posts/#findComment-1127496 Share on other sites More sharing options...
jarvis Posted October 28, 2010 Author Share Posted October 28, 2010 Thanks salathe, I'm more than happy to bin what's there. What I'd like is a simple script I can enter the RSS URL, set it to show X number of posts (newest first), set the number of words/chars and hey presto. If you know of a simple script, I'm all eyes! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/217089-rss-to-php-limit-description-no-posts/#findComment-1127502 Share on other sites More sharing options...
GalaxyTramp Posted October 28, 2010 Share Posted October 28, 2010 Try something like Magpie http://magpierss.sourceforge.net/ GT Quote Link to comment https://forums.phpfreaks.com/topic/217089-rss-to-php-limit-description-no-posts/#findComment-1127509 Share on other sites More sharing options...
jarvis Posted October 28, 2010 Author Share Posted October 28, 2010 Thanks GalaxyTramp I did look at that but couldnt get much sense out of it - must be a bit thick! Would rather try and code something myself ideally, easier in future to alter it etc Quote Link to comment https://forums.phpfreaks.com/topic/217089-rss-to-php-limit-description-no-posts/#findComment-1127510 Share on other sites More sharing options...
GalaxyTramp Posted October 28, 2010 Share Posted October 28, 2010 Hi Nobody is thick around here Magpie looks difficult at first glance but if you run through the supplied example it is fairly simple to execute. The first time I used it I created a static XML file with some simple content then added to this as I got to understand the workings more. Have a go at it you will always get advise here if you get stuck. GT Quote Link to comment https://forums.phpfreaks.com/topic/217089-rss-to-php-limit-description-no-posts/#findComment-1127512 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.