0
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="feedburner/ext/1.0" version="2.0">
<channel>
<title>NewsP</title>
<link>http://newscom/</link>

<description>News - Page A RSS</description>
<pubDate>Thu, 31 Oct 2013 16:22:54 GMT</pubDate>
<lastBuildDate>Thu, 31 Oct 2013 16:22:54 GMT</lastBuildDate>
<language>zh-TW</language>
<category>Newspaper</category>
<copyright>Copyright (C) 1998 News</copyright>



<image><title>news - Page A</title><url>logo1.jpg</url><link>dailydotcom/</link></image>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="pga" /><feedburner:info uri="pga" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://snufkinto.superfeedr.com/" /><geo:lat>22.1923</geo:lat><geo:long>113.5438</geo:long><creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/2.0/</creativeCommons:license><meta xmlns="http://pipes.yahoo.com" name="pipes" content="noprocess" /><item>

<title><![CDATA[A01.Ad - ]]></title>

<link>e_2.htm</link>
<description>&lt;img src=39451383243502891.jpg border=0&gt;&lt;div class="feedflare"&gt;
&lt;a href="pga?a=fry0sdpGTNM:pV-KRaLgwSI:yIl2AUoC8zA"&gt;&lt;img src="yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="fry0sdpGTNM:pV-KRaLgwSI:qj6IDK7rITs"&gt;&lt;img src="pga?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="fry0sdpGTNM" height="1" width="1"/&gt;</description>
<guid isPermaLink="false">node_2.htm</guid>
<pubDate>Thu, 31 Oct 2013 16:22:54 GMT</pubDate>
<category>Ad</category>
<author>com (Remus To)</author>
<feedburner:origLink>http://e_2.htm</feedburner:origLink></item>

<item>
<title><![CDATA[A02.Ad - ]]></title>
...
...
...
</channel>
</rss>

How to extract out the news <title>, <description>, <category>,using foreach etc.?

Here's my code:

<?php    

$movies = new SimpleXMLElement(file_get_contents("xml"));

foreach ($movies->channel->item->title as $title) {    
   echo $title, description, category,xxx,xxx,xxx, PHP_EOL;
   ####only one result echo!    
}    

?>

My code can only echo ONE result, i mean i have many new, how to get them all? thanks

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Parse ATOM feed with PHP: http://stackoverflow.com/questions/250679/best-way-to-parse-rss-atom-feeds-with-php Think the answer there with the 75 up-votes provides a good example. – ficuscr Nov 01 '13 at 14:56

1 Answers1

1

You are iterating over the first title only because you are iterating over the first items first title only.

Instead you want to iterate over all items and then get the title for each:

foreach ($movies->channel->item as $item)
{
    echo $item->title, PHP_EOL;
}

Output:

A01.Ad - 
A02.Ad - 

That's all, this is generally outlined in the PHP Simplexml Basic Usage Examples, I suggest you take a read there in case you get stuck.

hakre
  • 193,403
  • 52
  • 435
  • 836