php/regex: separate feed data
<description><img src='http://example.com/Pic_018_70x70.gif'></img><br />This is the content of the desc开发者_运维问答ription.</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?
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.
You could also use simplexml
$xml = simplexml_load_string($string);
$description = $xml->description;
精彩评论