从 <content:encoded> 获取图像 src [英] get image src from &lt;content:encoded&gt;

查看:20
本文介绍了从 <content:encoded> 获取图像 src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 <content:encoded> 获取一个 img url 并将该 url 插入到我的数据库中

Im trying to get an img url from <content:encoded> and inserting the url into my DB

但我似乎无法从 xml 文件中获得正确的信息-

But i can't seem to get correct information from the xml file-

还是不能用 simpleXML 检索数据?

or is it not possible to retrive the data with simpleXML?

这是我的 XML

<item>
  <title>Movietitle</title>
  <content:encoded><![CDATA[<p>
    <img class="aligncenter  wp-image-22085" src="movie-poster-694x1024.jpg" alt="Predestination 2014" width="475" height="701" /></p>
    <p><span id="more-22087"></span></p>
    <p>
    <a href="http://bit.ly/1za5mIz" target="_blank">
    <h4 style="text-align: left;">Release Info:</h4>
    Genre: Sci-Fi, Thriller<br />
    Quality: DVDRip<br />
    Language: English</p>]]>
    </content:encoded>
</item>

PHP

$feeds = array('http://xxxx.xml');
foreach( $feeds as $feed ) {

    $xml = simplexml_load_file($feed);

    foreach($xml->channel->item as $item) {

        $video_title = $item->title;
        $video_img=(string) $item->children($ns['content']);


        $sql = "INSERT INTO video (video_title, video_img, video_date) VALUES (:video_title, :video_img, NOW())";
        $query = $dbh->prepare($sql);
        $query->execute(array(
            ':video_title' => $video_title,
            ':video_img' => $video_img
        ));     
    } 
}

推荐答案

是的,有可能,只要跟进那个->children(),然后把内容当作HTML处理.在这种情况下,您可以使用 DOMDocument,然后只需使用 ->getAttribute('src') 即可获取图像标签的来源.

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.

示例:

$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.

这篇关于从 <content:encoded> 获取图像 src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆