tags:

views:

38

answers:

2
<description>&lt;img src='http://example.com/Pic_018_70x70.gif'&amp;gt;&amp;lt;/img&amp;gt;&amp;lt;br /&gt;This is the content of the description.</description>

If I have images coming in the description field of a feed, how can I separate the image into a variable and strip the noise and have the description left too?

A: 

This will give you the value of the img src attribute from the string in your question, and give you the text:

$sxml = new SimpleXMLElement(html_entity_decode($str));
$images = $sxml->xpath('//@src');
$text = $sxml->xpath('//description');

echo $images[0];  // http://example.com/Pic_018_70x70.gif
echo $text[0];    // This is the content of the description.
GZipp
A: 

You could also use simplexml

$xml = simplexml_load_string($string);
$description = $xml->description;
streetparade
@streetparade - This results in $description being an empty string. Did you test this? And how does $xml = simplexml_load_string($string); differ from $xml = new SimpleXMLElement($string); ?
GZipp
Yes sure but then he should post the whole xml i think or?
streetparade
Or what? Your answer is wrong given what he did post.
GZipp