Sunday 15 December 2019

php - Find IMG tag and get it's "src" from within a RSS feed node




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]

Some link
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 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


Answer



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 tag from it.



$content = $xml->content;
preg_match('/(]+>)/i', $content, $matches);
echo $matches[0];

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...