0

I am trying to incorporate my pin feed into my site. I have got it working but I need to amend what it shows as it's not quite working as intended.

What I need is to extract a certain piece of data from the description bit of the date.

This is the code I use to grab my XML feed:

<?php
$ch = curl_init("http://pinterest.com/1234/feed.rss");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);

if(isset($doc->channel))
{
    parseRSS($doc);
}
function parseRSS($xml)
{
    $cnt = 9;
    for($i=0; $i<$cnt; $i++)
    {
    $url    = $xml->channel->item[$i]->link;
    $img    = $xml->channel->item[$i]->description;
    $title  = $xml->channel->item[$i]->title;
    echo '<p><a href="'.$url.'" title="'.$title.'">'.$img.'</a></p>';
    }
}
?>

The problem is that description looks like below and all I want is the src value from it:

<description>&lt;p&gt;&lt;a href="/pin/1785432765530/"&gt;&lt;img src="http://media-cache-ec1.pinterest.com/upload/27099622222548513383_qJV62266Pf_b.jpg"&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;What it takes to Google’s.&lt;/p&gt;</description>

Is there a way to just get src="http://media-cache-ec1.pinterest.com/upload/270996666522513383_qJV6666Pf_b.jpg" out of the description and store it in $img or another variable?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
odd
  • 453
  • 2
  • 10
  • 21

3 Answers3

2

html_entity_decode and Simple HTML DOM parser could solve your problem.

(http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php)

user1190992
  • 669
  • 3
  • 8
1

Some RegExp would help you (PHP Manual, Wikipedia)

eg.: .*(src=".*[^"]").*

m93a
  • 8,866
  • 9
  • 40
  • 58
0

thanks all i used

    $cnt = 9;
    for($i=0; $i<$cnt; $i++)
    {
    $url    = $xml->channel->item[$i]->link;
    $img    = $xml->channel->item[$i]->description;
    $title  = $xml->channel->item[$i]->title;
    $pattern = '/src="([^"]*)"/';
    preg_match($pattern, $img, $matches);
    $src = $matches[0];
    unset($matches);
    //echo $src;
    echo '<p><a href="'.$url.'" title="'.$title.'"><img '.$src.'</img></a></p>';
    }
}
?>

thanks for the tips

odd
  • 453
  • 2
  • 10
  • 21