PDA

View Full Version : Limit Title Chars on RSS Display Box Script



progresst
10-11-2009, 12:46 AM
1) Script Title:
RSS Display Boxes

2) Script URL (on DD):
http://www.dynamicdrive.com/dynamicindex18/rssdisplaybox/index.htm

3) Describe problem:
Is there a way to limit the amount of characters for the title? Script runs great but some of the feed titles are too long and I need to limit them.

Thanks,

Tony:)

jscheuer1
10-11-2009, 04:32 AM
OK, not a PHP expert here, but after doing a bit of digging around and experimenting, it appears obvious that the title is output by the outputbody.php file.

If you were to replace it with this one:


<?
//Function for ouputting the body of each RSS item displayed (inside loop)- DynamicDrive.com
//For syntax pf bpdu, see: http://simplepie.org/docs/installation/from-scratch/ and http://simplepie.org/docs/reference/
//Function by default defines 3 different body outputs (templates). Modify or add additional templates as desired

function shortentitle($title, $len=10){
if(strlen($title) > $len)
echo substr($title, 0, $len - 6) . ' . . .';
else
echo $title;
}

function outputbody($item, $template=""){
if ($template=="" || $template=="default"){ //DEFAULT TEMPLATE
?>
<DIV class="rsscontainer">
<div class="rsstitle"><a href="<?php echo $item->get_permalink(); ?>"><?php shortentitle($item->get_title()); ?></a></div>
<div class="rssdate"><?php echo $item->get_date('d M Y g:i a'); ?></div>
<div class="rssdescription"><?php echo $item->get_description(); ?></div>
</DIV>
<?
} //end default template
else if ($template=="titles"){ //"TITLES" TEMPLATE
?>
<DIV class="rsscontainer">
<div class="rsstitle"><a href="<?php echo $item->get_permalink(); ?>" target="_new"><?php shortentitle($item->get_title()); ?></a></div>
<div>Category: <?php echo $item->get_category(); ?></div>
</DIV>
<?
} //end titles template
else if ($template=="titlesdates"){ //"TITLESDATES" TEMPLATE
?>
<DIV class="rsscontainer">
<span class="rsstitle"><a href="<?php echo $item->get_permalink(); ?>"><?php shortentitle($item->get_title()); ?></a></span>
<span class="rssdate"><?php echo $item->get_date('m/d/y g:i a'); ?></span>
</DIV>
<?
} //end titlesdates template
else if ($template=="mytemplatename"){ //"mytemplatename" TEMPLATE
?>
//DEFINE ADDITIONAL CUSTOM TEMPLATE(s) USING SAME LOGIC STRUCTURE AS ABOVE
//For syntax of template body, see SimplePie docs: http://simplepie.org/docs/installation/from-scratch/ and http://simplepie.org/docs/reference/
<?
}




else
die ("No template exists with such name!");
} //Closing function bracket
?>

You should be able to get what you want. Notice particularly the:


function shortentitle($title, $len=10){

near the beginning. The red 10 determines the maximum length for the title. All titles longer than that will have their last 6 characters up to 10 replaced with:


. . .

and the rest dropped. Obviously 10 characters are probably too short, so adjust that red number as desired. It will be the maximum length including the padding dots of any title. Any title shorter or equal to this number in length will appear in its entirety without the padding dots.

progresst
10-11-2009, 12:03 PM
Many thanks!