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
https://forums.phpfreaks.com/topic/237833-getting-string-with-odd-tag-name/
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;
}

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.

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;}

?> 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.