get image src from [duplicate]

2019-05-21 07:07发布

问题:

This question already has an answer here:

  • How to parse CDATA HTML-content of XML using SimpleXML? 2 answers

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

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

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

This is my 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
        ));     
    } 
}

回答1:

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.