You are here:  » Possible to limit length of the DESCRIPTION text?


Possible to limit length of the DESCRIPTION text?

Submitted by kevinkos on Wed, 2006-11-22 09:46 in

Hi, thanks for a great solution! I just wonder if it is possible to limit the number of characters shown in the DESCRIPTION text? Some RSS providers have extremely long descriptions that destroy the whole layot of my site (I am using MagicParser in a Mambo CMS module, shown on the right hand with quite small space dedicated to my newsfeeds)

Here is my current code:

<?php
  
require("MagicParser.php");
  
$counter 0;
  function 
myRecordHandler($item)
  {
    global 
$counter;
    
// increment counter
    
$counter++;
   print 
"<b><a href='".$item["LINK"]."'>".$item["TITLE"]."</a></b><br />";
   print 
"<p>".$item["DESCRIPTION"]."</p>";
   print 
"<i>".$item["PUBDATE"]."</i><br /><br />";
    
// return true when counter reaches 5
    
return ($counter == 5);
  }
  
$url "http://www.leonardo-energy.org/drupal/taxonomy/term/15/0/feed";
  
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

Submitted by support on Wed, 2006-11-22 16:29

Hi Kevin,

No problem - you can use PHP's substr() function to do this - here it is inserted into your code - simply change the number in the function to the length of text you want. Unfortunately there is no easy way to make it finish exactly within a word boundry...

<?php
  
require("MagicParser.php");
  
$counter 0;
  function 
myRecordHandler($item)
  {
    global 
$counter;
    
// increment counter
    
$counter++;
   print 
"<b><a href='".$item["LINK"]."'>".$item["TITLE"]."</a></b><br />";
   print 
"<p>".substr($item["DESCRIPTION"],0,250)."</p>";
   print 
"<i>".$item["PUBDATE"]."</i><br /><br />";
    
// return true when counter reaches 5
    
return ($counter == 5);
  }
  
$url "http://www.leonardo-energy.org/drupal/taxonomy/term/15/0/feed";
  
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

That would limit the description to 250 characters.

Hope this helps!
David.

Submitted by kevinkos on Thu, 2006-11-23 09:17

Thanks! Works fine!