Clearing RSS

Discussion in 'PHP' started by asgsoft, Oct 24, 2007.

  1. #1
    Hi

    Here is another PHP problem I have. I am trying to clean up an RSS feed so that I can get a list of the domains. I have the general idea of what to do but just can't do the PHP.

    Basically it would get the text between the <title></title> and echo it in a list.

    But how do you do that?

    here is the RSS feed I am using:

    <?xml version="1.0"?>
    <rss version="2.0">
    	<channel>
    		<title>SaveSpell :: Available domains</title>
    		<link>http://www.domain.com/</link>
    		<description>Expiring and expired word list domains</description>
    		<language>en-us</language>
    		<ttl>1</ttl>
    				<item>
    			<title>jyanen.com</title>
    			<category></category>
    			<pubDate>Wed, 24 Oct 2007 15:29:27 UTC</pubDate>
    			<link>http://www.domain.com/info/jyanen.com</link>
    			<description><![CDATA[<img src="http://www.domain.com/images/status/4.png" alt="" /> jyanen.com]]></description>
    		</item>
    
    				<item>
    			<title>tertis.net</title>
    			<category></category>
    			<pubDate>Wed, 24 Oct 2007 08:17:32 UTC</pubDate>
    			<link>http://www.domain.com/info/tertis.net</link>
    			<description><![CDATA[<img src="http://www.domain.com/images/status/2.png" alt="" /> tertis.net]]></description>
    		</item>
    
    				<item>
    			<title>poutek.com</title>
    			<category></category>
    			<pubDate>Wed, 24 Oct 2007 08:14:20 UTC</pubDate>
    			<link>http://www.domain.com/info/poutek.com</link>
    			<description><![CDATA[<img src="http://www.domain.com/images/status/2.png" alt="" /> poutek.com]]></description>
    		</item>
    
    				<item>
    			<title>lmass.com</title>
    			<category></category>
    			<pubDate>Wed, 24 Oct 2007 07:40:30 UTC</pubDate>
    			<link>http://www.domain.com/info/lmass.com</link>
    			<description><![CDATA[<img src="http://www.domain.com/images/status/2.png" alt="" /> lmass.com]]></description>
    		</item>
    
    			</channel>
    </rss>
    Code (markup):
    Any help would be appreciated.
     
    asgsoft, Oct 24, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $xml = simplexml_load_file('file.xml');
    
    foreach ($xml->xpath('channel/item') AS $item)
    {
    	echo $item->title, "<br />\n";
    }
    
    PHP:
     
    nico_swd, Oct 24, 2007 IP
    asgsoft likes this.
  3. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #3
    that works great.

    Thanks

    +rep comming your way
     
    asgsoft, Oct 24, 2007 IP
  4. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #4
    i uploaded this to my PHP4 server and its unable to detect the function

    whats the equivalent of it in PHP4?
     
    asgsoft, Oct 26, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Do you maybe have an option to change between PHP 4 and PHP 5 in your control panel? (If not, you should ask your host to upgrade because the support for PHP 4 ends this year - Source)

    If you don't have any of these options, I'll code up something else.
     
    nico_swd, Oct 26, 2007 IP
  6. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #6
    My host doesn’t have PHP5, but I’ll tell them about the end of support. Its worth pointing their attention to that.

    But for the time being would it be OK with you to have a look at coding something up for me?
     
    asgsoft, Oct 26, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    This should do it:
    
    $xml = file_get_contents('file.xml');
    
    preg_match('~<channel>(.*?)</channel>~si', $xml, $channel);
    preg_match_all('~<item>(.*?)</item>~si', $channel[1], $items);
    
    foreach ($items[1] AS $item)
    {
    	preg_match('~<title>(.*?)</title>~i', $item, $title);
    	echo "<p>{$title[1]}</p>\n";
    }
    
    PHP:
     
    nico_swd, Oct 26, 2007 IP
  8. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #8
    nothing shows when I change it.

    the thing is there is more than one title tag in the feed.
     
    asgsoft, Oct 26, 2007 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    I've tested it with the XML file you posted above and it works just fine for me. :confused:
     
    nico_swd, Oct 26, 2007 IP
  10. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #10
    I am getting nothing at all.

    just a blank page
     
    asgsoft, Oct 26, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Hm, let's try it this way:
    
    $xml = file_get_contents('file.xml');
    
    preg_match_all('~<title>(.*?)</title>~si', $xml, $titles);
    array_shift($titles[1]);
    
    foreach ($titles[1] AS $title)
    {
    	echo "<p>{$title}</p>\n";
    }
    
    PHP:
    Works for me too (tested).
     
    nico_swd, Oct 26, 2007 IP
  12. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #12
    This works great.

    Thanks
     
    asgsoft, Oct 26, 2007 IP