get image src from <content:encoded> [duplicate]

为君一笑 提交于 2019-12-06 04:19:05

Yes, it is possible, just follow up on that ->children(), then treat the content as HTML. You can use DOMDocument in this case, then just use ->getAttribute('src') to get the source of the image tag.

Example:

$xml = simplexml_load_file('http://axxomovies.org/feed', null, LIBXML_NOCDATA);
foreach ($xml->channel->item as $item) {
    $title = (string) $item->title;
    $content = $item->children('content', 'http://purl.org/rss/1.0/modules/content/');
    $html_string = $content->encoded;
    $dom = new DOMDocument();
    libxml_use_internal_errors(true);
    $dom->loadHTML($html_string);
    libxml_clear_errors();
    $img = $dom->getElementsByTagName('img')->item(1)->getAttribute('src');
    echo 'Title: ' . $title . '<br/>';
    echo 'Image source: ' . $img;
    echo '<hr/>';
}

Sidenote: You do not need to prepare every iteration. You can take that off inside and put it above the loop instead. You only need to prepare it once.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!