Hi all, I'm trying to parse an rss feed and only extract a certain item that has a specific title. The title I'm looking for is set by $_GET[''] and I whant alle the information from that item to be stored in a variable. It is very easy to parse and output the entire rss feed using simpleXML, but when I try to use an if-condition, such as if($this->title=$_GET['what']){echo $this->title;} Code (markup): I get alle the items only with the titles changed to the $_GET['what'] value. Here's my code: <?php // Load and parse the XML document $rss = simplexml_load_file('http://www.myfeedrss.com/feed); $title = $rss->channel->title; ?> <html xml:lang="en" lang="en"> <head> <title><?php echo $title; ?></title> </head> <body> <h1><?php echo $title; ?></h1> <?php // Here we'll put a loop to include each item's title and description foreach ($rss->channel->item as $item) { if($item->title=$_GET[who]){ echo "<h2><a href='" . $item->link . "'>" . $item->title . "</a></h2>"; echo "<p>" . $item->description . "</p>"; } } ?> </body> </html> Code (markup): I'm sure it's easily possible to do what I whant, but I cannot find any support for it anywhere, so I hope someone can help me out here. Thanks in advance
I'm not sure what you want to do, but check this example from the PHP manual I guess you can do the same and replace "//character" with the name of the XML element you want to select and then do whatever you want inside the foreach.
Hi nabil_kadimi and thanks for your reply. Each <item> in the rss feed contains a <title>, a <link>, and <description> and a <pubDate>. I can easilly parse the feed and display these elements as html. But I want to find an <item> that has a specifoc <title> and the display all the info from that <item> as html.
This worked on my testing server <?php $rss = simplexml_load_file('http://www.example.com/feed'); $item_title = $_GET['title']; $xpath = "//channel/item[./title = '$item_title']"; foreach ($rss->xpath($xpath) as $item) { echo $item->title; # ...or whatever you want break; # This assumes you need only one item } PHP: This requires that the $_GET['title'] matches the exact title on the rss feed, if you want to specify only a part of the title the script will need a lit bit more work