Jump to content

[SOLVED] Limit String Length


itsinmyhead

Recommended Posts

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;
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.