Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:03:25+00:00 2026-05-26T16:03:25+00:00

How do I skip the item in an rss feed if the description text

  • 0

How do I skip the item in an rss feed if the description text does not exist?

I tried with this line below but I still get an error,

if($x -> item($i) -> getElementsByTagName('description')) { then proceed }

when I the item in the rss feed appears to be like this,

<item>
    <title>Berlusconi on the brink</title>
    <link>http://www.spectator.co.uk/coffeehouse/7375408/berlusconi-on-the-brink.thtml</link>
    <guid isPermaLink="false"> http://www.spectator.co.uk/coffeehouse/7375408/berlusconi-on-the-brink.thtml </guid>
    <pubDate>Tue, 08 Nov 2011 16:42:03 +0000</pubDate>
</item>

As you can see that <description> not is not provided.

Below is the function I came up with…

function feed_reader($url,$limit = 1,$title =  null)
{
    $xml = ($url); //http://twitter.com/statuses/friends_timeline/321998072.rss
    $xmlDoc = new DOMDocument();
    $xmlDoc -> load($xml);

    # Get and output "<item>" elements.
    $x = $xmlDoc -> getElementsByTagName('item');

    # Count the total feed with xpath.
    $xpath = new DOMXPath($xmlDoc);
    $total_feed = $xpath->evaluate('count(//item)');

    # Set feed limit.
    $limit_feed = $limit;

    # Check if the total feed is less than the limit then use the total feed as the limit.
    if($limit_feed >= $total_feed) $limit_feed = $total_feed;

    # Set the variable.
    $output = null;

    for ($i=0; $i<$limit_feed; $i++)
    {
                # Make sure that the description node exist then process.
        if($x -> item($i) -> getElementsByTagName('description'))
        {
            $item_title = $x -> item($i) -> getElementsByTagName('title') -> item(0) -> childNodes -> item(0) -> nodeValue;
            $item_link = $x -> item($i) -> getElementsByTagName('link') -> item(0) -> childNodes -> item(0) -> nodeValue;
            $item_date = $x -> item($i) -> getElementsByTagName('pubDate') -> item(0) -> childNodes -> item(0) -> nodeValue;
            $item_description = $x -> item($i) -> getElementsByTagName('description') -> item(0) -> childNodes -> item(0) -> nodeValue;

            # NOTE: use this code for the server runs PHP5.3
            # DateTime::add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
            $date = new DateTime($item_date);

            # change the date format into Y-m-d H:i:s
            $item_date = $date -> format('j F Y');

            # count time ago from the published date
            $time_ago = time_ago($date -> format('Y-m-d H:i:s'),'d M Y \a\t H:i');

            if($title) $output .= '<li><p>'.preg_replace('/^('.$title.':)/', ' ',limit_length(strip_tags($item_description), 200)).'<br/>'.$time_ago.'</p></li>';
                else $output .= '<li><p>'.limit_length(strip_tags($item_description), 200).'<br/>'.$time_ago.'</p></li>';

        }
    }

    return $output;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:03:26+00:00Added an answer on May 26, 2026 at 4:03 pm

    Just use a single xpath query to return all items that do have the description (as you look only for those):

    $xmlDoc = new DOMDocument();
    $xmlDoc->loadXML($xml);
    $xpath = new DOMXPath($xmlDoc);
    $items = $xpath->query('/rss/item/description/..');
    
    # Set the variable.
    $output = '';
    
    foreach($items as $number => $item)
    {
        if ($number+1 >= $limit_feed) break; # enough items, limit reached
    
        # process each item as you see fit:
    
        echo $xmlDoc->saveXML($item), "\n"; # debug only for demo
    
        $item_title = $item->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
    
        # and so on ....
    
        if ($title) 
            $output .= '<li><p>'.preg_replace('/^('.$title.':)/', ' ',limit_length(strip_tags($item_description), 200)).'<br/>'.$time_ago.'</p></li>';
        else
            $output .= '<li><p>'.limit_length(strip_tags($item_description), 200).'<br/>'.$time_ago.'</p></li>';
    
    }
    
    return $output;
    

    Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to skip to the first line that contains include. <> until /include/;
You can skip to the bottom line if you don't care about the background:
If I skip, do I still need to read the end element? reader.ReadStartElement(ENCODING); reader.Skip();
Is there an attribute to skip empty arrays in the xml-serialization of c#? This
I am retrieving 10 rown from my database but I want to skip the
Skip to the bottom for the question; this is just some extra info I
I have the macro below, everything I read states it should work, but when
Iam trying to parse rss feed from particular url... here is my php file
The code below (for running in LinqPad) is meant to parse the foo/skip/bar string
SKIP TO UPDATE 3 I want to enable the Seam debug page on Weblogic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.