开发者

php/regex: separate feed data

开发者 https://www.devze.com 2022-12-19 07:56 出处:网络
<description>&lt;img src=\'http://example.com/Pic_018_70x70.gif\'&gt;&lt;/img&gt;&lt;br /&gt;This is the content of the desc开发者_运维问答ription.</description>
<description>&lt;img src='http://example.com/Pic_018_70x70.gif'&gt;&lt;/img&gt;&lt;br /&gt;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;
0

精彩评论

暂无评论...
验证码 换一张
取 消