0

This may be a very basic question. So please excuse me for the noobness. I'm trying to get myself familiar with xml traversing. Suppose I've this node

[content]
 <img src="some url" />
 <a href="some link">Some link</a>
 Some text after the link.
[/content]

As you can see, the node contains a mixture of text and tags. So I was wondering if I could target the img tag within that node and get it's src attribute?

I'm using simplexml to read the xml file.

If I do just $xml->content, the browser is showing an image, a link and the text. So I was hoping there was some option to "find" the <img> tag within the content node.

UPDATE

Okay. I think I might have used the wrong technical terms. Are RSS feeds are XML one and the same? I'm getting the XML feed from this URL

asprin
  • 9,579
  • 12
  • 66
  • 119
  • You are looking for HTML inside CDATA (text) elements of the RSS Feed XML. For the HTML, you find the answer in a duplicate question. For the XML, there are *tons* of duplicate quesitons. Stackoverflow works best asking one question at a time ;) – hakre Feb 06 '13 at 00:58

3 Answers3

3

I got it figured it out myself. What I did was take the entire content of the [content] node and then use preg_match to find the <img> tag from it.

$content = $xml->content;
preg_match('/(<img[^>]+>)/i', $content, $matches);
echo $matches[0];
asprin
  • 9,579
  • 12
  • 66
  • 119
0

At the moment content is not a XML node. It should be formed like this;

<content></content>

To get the image source just do;

$xml->content->img['src']

Simplexml makes nodes accessible via '->'. Node attributes are accessible via array notation '["attr name"]'

Hope this helps you

mvbrakel
  • 936
  • 5
  • 16
0

This should help you:

<?php

    $html = '
        <body>
            <img src="some url" />
            <a href="some link">Some link</a>
            Some text after the link.
        </body>
    ';

    $xml = simplexml_load_string($html);
    foreach($xml->children() as $child)
    {
        if ($child->getName() === 'img')
        {
            $attributes = $child->attributes();
            $img_source = $attributes['src'];
            echo $img_source;
        }
    }
?>
Dale
  • 10,384
  • 21
  • 34