Jump to content

RSS to PHP - Limit description & no posts


jarvis

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.