itsinmyhead Posted March 30, 2009 Share Posted March 30, 2009 I made a few searches and couldn't find anything that helped me out. Maybe someone here can lend some advice. I'm creating an RSS feed for our classified ad listings, and I want to limit the description of the results to a certain number of characters - let's say 20. I played around with substr() and other things, but nothing seems to have any affect on the results whatsoever. I don't want any words to be cut in half - I want the last word in a description to be displayed followed by "..." if necessary. Here's what my code looks like right now: <?php $database = "db"; $dbconnect = mysql_connect("localhost","user","pw"); mysql_select_db($database, $dbconnect); $query = "select id, category, description from classifieds order by rand()"; $result = mysql_query($query, $dbconnect); while ($line = mysql_fetch_assoc($result)) { $return[] = $line; } $now = date("D, d M Y H:i:s T"); $output = "<?xml version=\"1.0\"?> <rss version=\"2.0\"> <channel> <title>Adirondack Pennysaver Classified Listings</title> <link>http://www.adkpennysaver.com</link> <description>Classified Ad Listings for the Adirondack Pennysaver in Plattsburgh, New York.</description> <language>en-us</language> <pubDate>$now</pubDate> <lastBuildDate>$now</lastBuildDate> "; foreach ($return as $line) { $output .= "<item><title>".htmlentities($line['category'])." - ".htmlentities(strip_tags($line['description']))."</title> <link>".htmlentities($line['http://www.adkpennysaver.com'])."</link> </item>"; } $output .= "</channel></rss>"; header("Content-Type: application/rss+xml"); echo $output; ?> Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted March 30, 2009 Share Posted March 30, 2009 I haven't made a function for this myself, but a quick google search brought me to this guy's blog: http://www.milesj.me/blog/read/15/5-custom-basic-php-string-functions First one seems to be what you're looking for Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 This is one way I've found to do this using already existing functions. <?php $database = "db"; $dbconnect = mysql_connect("localhost","user","pw"); mysql_select_db($database, $dbconnect); $query = "select id, category, description from classifieds order by rand()"; $result = mysql_query($query, $dbconnect); while ($line = mysql_fetch_assoc($result)) { $return[] = $line; } $now = date("D, d M Y H:i:s T"); $output = "<?xml version=\"1.0\"?> <rss version=\"2.0\"> <channel> <title>Adirondack Pennysaver Classified Listings</title> <link>http://www.adkpennysaver.com</link> <description>Classified Ad Listings for the Adirondack Pennysaver in Plattsburgh, New York.</description> <language>en-us</language> <pubDate>$now</pubDate> <lastBuildDate>$now</lastBuildDate> "; foreach ($return as $line) { $desc = htmlentities(strip_tags($line['description'])); $lines = wordwrap($desc, 20);//20 characters, change as you'd like $desc = $lines[0]; if(count($lines > 1)){ $desc .= "..."; } $output .= "<item><title>".htmlentities($line['category'])." - ".$desc."</title> <link>".htmlentities($line['http://www.adkpennysaver.com'])."</link> </item>"; } $output .= "</channel></rss>"; header("Content-Type: application/rss+xml"); echo $output; ?> </html> Quote Link to comment Share on other sites More sharing options...
itsinmyhead Posted March 30, 2009 Author Share Posted March 30, 2009 I haven't made a function for this myself, but a quick google search brought me to this guy's blog: http://www.milesj.me/blog/read/15/5-custom-basic-php-string-functions First one seems to be what you're looking for I don't know how I didn't come across his blog when I searched earlier, but oh well. It looks like what I'd need, but I can't figure out how to implement it into my existing code. I tried Brian's suggestion, too, and it displayed zero results. I refreshed a couple of times to make sure and it displayed one letter followed by an ellipsis, but after another refresh it went back to displaying no results. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 just to throw in my 2c, here is a function that i wrote. it will find the first whitespace after 50 characters and trim the string down to that length <?php function echoLink($row) { if(strlen($row['test']) > 50) { //find the first position after 50 that is whitespace $abstract = $row['test']; $ws = strpos($abstract,' ',50); if($ws != 0) { $abstract = substr($abstract,0,$ws)." ..."; } } else { $abstract = $row['test']; } return $abstract; } Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 30, 2009 Share Posted March 30, 2009 woops, I left out the most important part <?php $database = "db"; $dbconnect = mysql_connect("localhost","user","pw"); mysql_select_db($database, $dbconnect); $query = "select id, category, description from classifieds order by rand()"; $result = mysql_query($query, $dbconnect); while ($line = mysql_fetch_assoc($result)) { $return[] = $line; } $now = date("D, d M Y H:i:s T"); $output = "<?xml version=\"1.0\"?> <rss version=\"2.0\"> <channel> <title>Adirondack Pennysaver Classified Listings</title> <link>http://www.adkpennysaver.com</link> <description>Classified Ad Listings for the Adirondack Pennysaver in Plattsburgh, New York.</description> <language>en-us</language> <pubDate>$now</pubDate> <lastBuildDate>$now</lastBuildDate> "; foreach ($return as $line) { $desc = htmlentities(strip_tags($line['description'])); $lines = explode("\n", wordwrap($desc, 20));//20 characters, change as you'd like $desc = $lines[0]; if(count($lines > 1)){ $desc .= "..."; } $output .= "<item><title>".htmlentities($line['category'])." - ".$desc."</title> <link>".htmlentities($line['http://www.adkpennysaver.com'])."</link> </item>"; } $output .= "</channel></rss>"; header("Content-Type: application/rss+xml"); echo $output; ?> </html> I left out the explode by \n lol Quote Link to comment Share on other sites More sharing options...
itsinmyhead Posted March 30, 2009 Author Share Posted March 30, 2009 Brian, you're awesome! Works like a charm. Thanks so much, everyone! (I would still be interested in knowing how the script in the blog works, Monkeh, if you have any ideas on implementing it into my existing code) Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 30, 2009 Share Posted March 30, 2009 I came up with this simple version.... <?php function cut_str($str, $len = 20){ if(strlen($str) > $len){ $string = ''; while($str{$len} != ' '){ $string .= $str{$len}; $len++; } return substr($str,0,$len).'...'; } return $str; } $str = 'hello this is a 20 char at str.'; echo cut_str($str); ?> Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted March 30, 2009 Share Posted March 30, 2009 Haven't tested it but I think this would do it: foreach ($return as $line) { $description = truncate($line['description'], 20); $output .= "<item><title>".htmlentities($line['category'])." - ".htmlentities($description)."</title> <link>".htmlentities($line['http://www.adkpennysaver.com'])."</link> </item>"; } Of course you'd need to put the function truncate() from the blog in your file somewhere, maybe in an include near the top of the page. Quote Link to comment Share on other sites More sharing options...
itsinmyhead Posted March 31, 2009 Author Share Posted March 31, 2009 I've got another question unrelated to the topic, but thought I'd post it here rather than starting a whole new one since people have already been so speed in replying here. Everything is working well, except when there are no ads in a specific category. For instance: http://adkpennysaver.com/rss/rssrea.php (Real Estate - has a decent number of ads to display) http://adkpennysaver.com/rss/rsshelp.php (Help Wanted - no ads to display) If you go to the Help Wanted page, you can see that it just gives an error. Is there any way to get it to display the regular "Subscribe to this feed" page as it does with the Real Estate example linked? I know there aren't any ads listed currently, but I would like people to be able to subscribe to them either way. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 just check the size of the array before you try to step through it with foreach. if the array is empty, dont display anything except the subscribe button Quote Link to comment Share on other sites More sharing options...
itsinmyhead Posted March 31, 2009 Author Share Posted March 31, 2009 I can't seem to find a way to get this to work... I either find myself winding up with the same error, or it returns telling me that the result set isn't empty. Did some Googling, of course, but haven't come across a surefire fix. Quote Link to comment Share on other sites More sharing options...
Brian W Posted March 31, 2009 Share Posted March 31, 2009 use count() on the array, if it returns < 1 then there are no adds for the section. walla Quote Link to comment 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.