title: Limit number of RSS feed items PHP
body: <p>I have found a nice script for parsing a twitter feed here <a href="https://github.com/karlmonson/php-xml-twitter-feed/blob/master/php-xml-twitter-feed.php" rel="nofollow">https://github.com/karlmonson/php-xml-twitter-feed/blob/master/php-xml-twitter-feed.php</a>, however I would need to add a customizable limit as to how many RSS entries are shown (for example 3 or 5). How would I do that and where would I need to insert the line of code?</p>
  
  <p>I have some other PHP scripts which use lines like:</p>
  
  <p>$maxitems = 10;
  $items = array_slice($rss->items, 0, $maxitems);</p>
  
  <p>...but I have no idea of how to combine this with the scripts below.</p>
  
  <p>Any help or tips are much appreciated!</p>
  
  <p>The code from the link looks like this:</p>
  
  <p>Part 1:</p>
  
  <pre><code>&lt;?php
  // Create the human readable "ago" time for each Tweet
  function humanTiming ($time) {
  
      // Get the current time set to GMT
      $cur_date = strtotime(gmdate("M d Y H:i:s O"));
  
      // Get the time since Tweet was tweeted
      $time = $cur_date - $time;
  
      // Set the tokens in seconds to calculate which "ago" string to return
      $tokens = array (
          31536000 =&gt; 'YEAR',
          2592000 =&gt; 'MONTH',
          604800 =&gt; 'WEEK',
          86400 =&gt; 'DAY',
          3600 =&gt; 'HOUR',
          60 =&gt; 'MINUTE',
          1 =&gt; 'SECOND'
      );
  
      // Count the seconds and return the "ago" string
      foreach ($tokens as $unit =&gt; $text) {
          if ($time &lt; $unit) continue;
          $numberOfUnits = floor($time / $unit);
          $returnValue = $numberOfUnits.' '.$text.(($numberOfUnits&gt;1)?'S':'');
          return $returnValue;
      }
  
  }?&gt;
  </code></pre>
  
  <p>Part 2:</p>
  
  <pre><code>&lt;?php
      // Set the feed url. Replace YourTwitterFeed with your Twitter Username
      $feed = simplexml_load_file('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=YourTwitterFeed');
  
      echo "&lt;ul&gt;";
  
      if (isset($feed)) :
          // Loop through each feed item and display each item as text with hyperlinks included.
          foreach ( $feed-&gt;channel-&gt;item as $tweet ) : 
              // Get the date the Tweet was published
              $pubDate = date(strtotime($tweet-&gt;pubDate));
              // Get the text of the Tweet
              $text = htmlspecialchars($tweet-&gt;description);
              // Filter each Tweet to make clickable handles and links
              $text = preg_replace('@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@', '&lt;a href="$1" rel="nofollow" target="_blank"&gt;$1&lt;/a&gt;', $text);
              $text = preg_replace('/@(\w+)/','&lt;a href="http://twitter.com/$1" target="_blank"&gt;@$1&lt;/a&gt;',$text);
              $text = preg_replace('/\s+#(\w+)/',' &lt;a href="http://search.twitter.com/search?q=%23$1" target="_blank"&gt;#$1&lt;/a&gt;',$text);
              // Get the human readable "ago" time
              $humantime = humanTiming($pubDate);
  
              // Return each Tweet in a list element
              echo "&lt;li&gt;";
                  echo $text;
                  echo "&lt;br /&gt;";
                  echo "&lt;small&gt;&lt;i&gt;TWEETED " . $humantime . " AGO&lt;/i&gt;&lt;/small&gt;";
              echo "&lt;/li&gt;";
  
          endforeach; 
      else :
          // Handler for an empty list of Tweets
          echo '&lt;li&gt;No items.&lt;/li&gt;';
      endif; 
  
      echo "&lt;/ul&gt;";
  ?&gt;
  </code></pre>
  
  <p>Thank you very much !</p>