ErickBest ErickBest - 1 year ago 114
PHP Question

PHP get img src from xml

I have a page with xml that looks like:

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">
<title>FB-RSS feed for Salman Khan Fc</title>
<description>FB-RSS feed for Salman Khan Fc</description>
<managingEditor> (FB-RSS)</managingEditor>
<pubDate>31 Mar 16 20:00 +0000</pubDate>
<title>Photo - Who is the Best Khan ?</title>
<description>&lt;a href=&#34;;&gt;&lt;img src=&#34;;oe=57BB41D5&#34;&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;Who is the Best Khan ?</description>
<pubDate>31 Mar 16 20:00 +0000</pubDate>
<description>&lt;a href=&#34;;&gt;&lt;img src=&#34;;oe=57778068&#34;&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;</description>
<pubDate>31 Mar 16 19:58 +0000</pubDate>

I want to get the
s of the
s in the

The images are stored in the
however, they are not in the format of


they rather look like:

&lt;img src=&#34;https://scontent.xx.fbc...

is replace with
... I guess thats why
$imgs = $dom->getElementsByTagName('img');
returns nothing.

Is there any work around?

This is how I call it:

$dom = new DOMDocument();
$dom->loadXML( $xml_file);
$imgs = ...(get the imgs to extract the src...('img') ??;

//Then run a possible foreach
//something like:

foreach($imgs as $img){

$src= ///the src of the $img

//try it out
echo '<img src="'.$src.'" /> <br />',

Any Idea?

Answer Source

You have HTML embedded in XML tags, so you have to retrieve XML nodes, load each HTML and retrieve desired tag attribute.

In your XML there are different <description> nodes, so using ->getElementsByTagName will return more than your desired nodes. Use DOMXPath to retrieve only <description> nodes in the right tree position:

$dom = new DOMDocument();
libxml_use_internal_errors( True );
$dom->loadXML( $xml );
$dom->formatOutput = True;

$xpath = new DOMXPath( $dom );
$nodes = $xpath->query( 'channel/item/description' );

Then iterate all nodes, load node value in a new DOMDocument (no need to decode html entities, DOM already decodes it for you), and extract src attribute from <img> node:

foreach( $nodes as $node )
    $html = new DOMDocument();
    $html->loadHTML( $node->nodeValue );
    $src = $html->getElementsByTagName( 'img' )->item(0)->getAttribute('src');
} demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download