Results 1 to 3 of 3

Thread: RSS Feed Display - Word Limit

  1. #1
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default RSS Feed Display - Word Limit

    Managed to get some php written that will display a feed on my homepage. Works great, but I can only figure out how to limit the number of characters from a description, not words. Can anyone give me some pointers on including some code for a "number of words" limiter? Thanks!

    PHP Code:

    <?php
        $rss 
    = new DOMDocument();
        
    $rss->load(http://wordpress.org/news/);
        
    $feed = array();
        foreach (
    $rss->getElementsByTagName('item') as $node) {
            
    $item = array ( 
                
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                
    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
                
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                );
            
    array_push($feed$item);
        }
        
    $limit 3;
        for(
    $x=0;$x<$limit;$x++) {
            
    $title str_replace(' & '' &amp; '$feed[$x]['title']);
            
    $link $feed[$x]['link'];
            
    $description $feed[$x]['desc'];
            
    $description substr($description0200);
            
    $date date('l F d, Y'strtotime($feed[$x]['date']));
            echo 
    '<p><font color=3c3c3c link=3c3c3c face=arial size=2><a href="'.$link.'" title="'.$title.'">'.$title.'</a></font><br />';
            echo 
    '<font color=3c3c3c face=arial size=1><em>Posted on '.$date.'</em></font><br />';
            echo 
    '<font color=3c3c3c face=arial size=2>'.$description.' ...</font></p>';
        }
    ?>

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Interesting. Well, assuming all words are separated by a space " " you could explode the string with the space as the delimiter, which would insert the words into an array. Then you can just truncate the array to your limit.

    Also, you have a syntax error at your second line because the URL is not contained as a string. I fixed that for you.

    Try this:
    PHP Code:
    <?php
        $rss 
    = new DOMDocument();
        
    $rss->load("http://wordpress.org/news/");
        
    $feed = array();
        foreach (
    $rss->getElementsByTagName('item') as $node) {
            
    $item = array ( 
                
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                
    'desc' => $desc,
                
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                );
            
    array_push($feed$item);
        }
        
    $limit 3;
        for(
    $x=0;$x<$limit;$x++) {
            
    $title str_replace(' & '' &amp; '$feed[$x]['title']);
            
    $link $feed[$x]['link'];
            
    $description $feed[$x]['desc'];
            
    $description substr($description0200);
            
            
    $limit_of_words 20// The number of words you want to limit by.
            
    $description explode(" "$description);
            
    $description array_slice($description0$limit_of_words);
            
    $description implode(" "$description); // Glue the pieces back together.
            
            
    $date date('l F d, Y'strtotime($feed[$x]['date']));
            echo 
    '<p><font color=3c3c3c link=3c3c3c face=arial size=2><a href="'.$link.'" title="'.$title.'">'.$title.'</a></font><br />';
            echo 
    '<font color=3c3c3c face=arial size=1><em>Posted on '.$date.'</em></font><br />';
            echo 
    '<font color=3c3c3c face=arial size=2>'.$description.' ...</font></p>';
        }
    ?>

  3. #3
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ah, thanks. I actually got this to work with just a couple of lines.
    Here's the final code. Makes $shortdesc but truncating the output of $description with trim, you can set the number of words - here it's 15.

    PHP Code:
    <?php 
        $rss 
    = new DOMDocument(); 
        
    $rss->load('http://wordpress.org/news/feed/'); 
        
    $feed = array(); 
        foreach (
    $rss->getElementsByTagName('item') as $node) { 
            
    $item = array (  
                
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue
                
    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue
                
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue
                
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
                ); 
            
    array_push($feed$item); 
        } 
        
    $limit 3
        for(
    $x=0;$x<$limit;$x++) { 
            
    $title str_replace(' & '' &amp; '$feed[$x]['title']); 
            
    $link $feed[$x]['link']; 
            
    $description $feed[$x]['desc']; 
            
    $description substr($description0200); 

      
    $numwords 15;
      
    preg_match("/(\S+\s*){0,$numwords}/"$description$regs);
      
    $shortdesc trim($regs[0]);

            
    $date date('l F d, Y'strtotime($feed[$x]['date'])); 
            echo 
    '<p><font color=3c3c3c link=3c3c3c face=arial size=2><a href="'.$link.'" title="'.$title.'">'.$title.'</a></font><br />'
            echo 
    '<font color=3c3c3c face=arial size=1><em>Posted on '.$date.'</em></font><br />'
            echo 
    '<font color=3c3c3c face=arial size=2>'.$shortdesc.' ...</font></p>'
        } 
    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •