simplexml_load_file()不显示图像php

问题描述 投票:0回答:1

我正在从日记中检索本地新闻,它在description标记中有图像,但是没有出现在结果中。他们甚至都没有出现在代码中

 $feeds = array("https://libero.pe/rss/futbol-peruano");

    //Read each feed's items
    $entries = array();

    foreach($feeds as $feed) {
        $xml = simplexml_load_file($feed);
        $entries = array_merge($entries, $xml->xpath("//item"));

    }

HTML

         $i=1;
         foreach($entries as $entry){
            if($i<5){
         ?>
         <li><a href="<?= $entry->link ?>"><?= $entry->title ?></a> (<?= parse_url($entry->link)['host'] ?>)
        <p><?= strftime('%m/%d/%Y %I:%M %p', strtotime($entry->pubDate)) ?></p>
        <p><?= $entry->description ?></p></li>
        <?php
        $i++;
       }
       }

我试图添加xpath(),但没有成功,我在做什么错?

   foreach( $xml->xpath('//image') as $image)
  {
     $attributes = $image->attributes();
    echo $attributes['path'] . "<br />";
  }
php xml
1个回答
0
投票

主要问题只是内容周围的空白,要添加的一个简单的事情是在处理各种元素之前使用trim()围绕各种元素...

 ?>
 <li><a href="<?= trim($entry->link) ?>"><?= trim($entry->title) ?></a> (<?= parse_url(trim($entry->link))['host'] ?>)
<p><?= strftime('%m/%d/%Y %I:%M %p', strtotime(trim($entry->pubDate))) ?></p>
<p><?= trim($entry->description) ?></p></li>
<?php
© www.soinside.com 2019 - 2024. All rights reserved.