Jump to content

getting string with odd tag name


nickjonnes

Recommended Posts

hey all,

i am trying to get the first url from a rss feed

this is the rss feed link:

'http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml'

i think the issue may be to do with two things

first it is a child of the item tag

second it has an odd tag name ie. <guid isPermaLink="false"><guid>

 

the feed looks like this

<item>
<title>this is a title</title>
<link>link to the article</link>
<guid isPermaLink="false">http://www.cnn.com/video/#/video/bestoftv/2011/05/27/arena.mets.cnn</guid>
<description>d</description>
<category>bestoftv</category>
<pubDate>Sat, 28 May 2011 01:39:51 GMT</pubDate>
<feedburner:origLink>http://www.cnn.com/video/#/video/bestoftv/2011/05/27/arena.mets.cnn</feedburner:origLink>
</item>

 

my php code to grab the video link

<?php
if (file_exists('http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml')) {

$xmldata = file_get_contents("http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml"); 

$url = value_in('guid isPermaLink="false', $xmldata); 
  print_r($url);
} else {
    exit('Failed to open xml data');
}
?>

i hope someone can help as i have been trying hard to fix this issue

cheers nick

Link to comment
Share on other sites

oh sorry shouild have said

function value_in($element_name, $xml, $content_only = true) {
    if ($xml == false) {
        return false;
    }
    $found = preg_match('#<'.$element_name.'(?:\s+[^>]+)?>(.*?)'.
            '</'.$element_name.'>#s', $xml, $matches);
    if ($found != false) {
        if ($content_only) {
            return $matches[1];  //ignore the enclosing tags
        } else {
            return $matches[0];  //return the full pattern match
        }
    }
    // No match found: return false.
    return false;
}

Link to comment
Share on other sites

I'm too lazy to look it up, but the code would look something like this:

$dom = new DOMDocument();
$dom->load("url");

$elements = $dom->getElementsByTagName("guid");
foreach ($element in $elements) {
    echo $element->innerHTML;
}

The PHP Manual is your friend.

Link to comment
Share on other sites

i fixed it, didnt have the $ sign at start but now im getting "unexspected T_string"

<?php
$dom = new DOMDocument('1.0','ISO-8859-1');
$dom->load("http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml");

$elements = $dom->getElementsByTagName("guid");
for each ($element in $elements)
{echo $element->innerHTML;}

?> 

Link to comment
Share on other sites

You might also like to use SimpleXML, which is supposed to make accessing XML "simple". To get the first link from within a feed item, you can simply do:

 

$feed = simplexml_load_file('http://feeds.bbci.co.uk/news/video_and_audio/science_and_environment/rss.xml');
$link = (string) $feed->channel->item->link;

echo $link;

 

Be sure to read through the examples page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.