2017-08-14 10 views
0

descriptionタグからデータを取得する際に助けが必要です。<a>,<img>などのテキストで構成されています。私が解析しようとしているXMLはthisxmlのタグ内でhtmlを解析する方法

というタグが記述テキストとともに得られたdescriptionタグを除き、必要なすべてのデータを得ることができました。私が必要なのはimgのsrcとdescriptionのテキストです。

マイコード:

foreach ($rss->getElementsByTagName('item') as $node) { 
     /*$test = $node->getElementsByTagName('description'); 
     $test = $test->item(0)->textContent;*/ 
     var_dump($test); 
     exit; 
     $nodes = $node->getElementsByTagName('content'); 


     if(!is_object($nodes) || $nodes === null || $nodes->length==0){ 

       $linkthumbNode = $node->getElementsByTagName('image'); 


       if(isset($linkthumbNode) && $linkthumbNode->length >0){ 
         $linkthumb=$linkthumbNode->item(0)->nodeValue; 

         if(empty($linkthumb)||$linkthumb == " "){ 


          $linkthumb = $linkthumbNode->item(0)->getAttribute('src'); 

         } 

        }else{ 

         $linkthumb = "NO IMAGE"; 
       } 

     }else{ 

      $linkthumb = $nodes->item(0)->getAttribute('url'); 
     } 

     $title = $node->getElementsByTagName('title')->item(0)->nodeValue; 
     $desc = $node->getElementsByTagName('description')->item(0)->textContent; 
     $link = $node->getElementsByTagName('link')->item(0)->nodeValue; 
     $img = $linkthumb; 
     $date = $node->getElementsByTagName('pubDate'); 
     if(isset($date) && $date->length >0){ 
      $date = $date->item(0)->nodeValue; 
     }else{ 
      $date = "no date provided"; 

     } 


     $item = array ( 
      'title' => $title, 
      'desc' => $desc, 
      'link' => $link, 
      'img' => $img, 
      'date' => $date, 
      ); 
     array_push($feed, $item); 
    } 

XML記述タグは次のとおりです。

<description> 
<a href="http://timesofindia.indiatimes.com/life-style/health-fitness/diet/9-food-combos-to-make-you-lean/articleshow/20984744.cms"><img border="0" hspace="10" align="left" style="margin-top:3px;margin-right:5px;" src="http://timesofindia.indiatimes.com/photo/20984744.cms" /></a>Nine food combinations that will make staying healthy and looking fit easier 
</description> 

私は必要なもの:画像としてhttp://timesofindia.indiatimes.com/photo/20984744.cms、私の説明などNine food combinations that will make staying healthy and looking fit easier

誰かが私を助けることができますか? PHPでXMLを解析するのはそれほど素晴らしいことではありません。

答えて

0

多分私はパーティーに少し遅れましたが、まだ答えが必要な場合は、私の解決策をチェックしてください。私はPHPのDOMDocumentと正規表現を使用しています。なぜなら、XML拡張だけを使って必要なデータを取得する簡単な方法が見つからないからです。

$rss = file_get_contents('https://timesofindia.indiatimes.com/rssfeeds/2886704.cms'); 
$feed = new DOMDocument(); 
$feed->loadXML($rss); 

$items = array(); 

foreach($feed->getElementsByTagName('item') as $item) { 
    $arr = array(); 
    foreach($item->childNodes as $child) { 
     if($child->nodeName === 'title' || $child->nodeName === 'link') $arr[$child->nodeName] = $child->nodeValue; 
     if($child->nodeName === 'pubDate') $arr['date'] = $child->nodeValue; 
     if($child->nodeName === 'description') { 
      preg_match('/(?<=src=[\'\"])(.+)(?=[\'\"])/i', $child->nodeValue, $matches); 
      $arr['img'] = $matches[0]; 
      preg_match('/[^>]+$/i', $child->nodeValue, $matches); 
      $arr['desc'] = $matches[0]; 
     } 
    } 
    array_push($items, $arr); 
} 
print_r($items); 

出力は次のようであり、あなたが必要なもののようだ:

Array ([0] => Array ([title] => 5 reasons you get sore after sex [img] => https://timesofindia.indiatimes.com/photo/61101815.cms [desc] => Sometimes, a super-filmy, almost-perfect sex leaves you all euphoric but only to end with soreness later. So, what is it that is going wrong? Can it be remedied? [link] => https://timesofindia.indiatimes.com/life-style/health-fitness/health-news/5-reasons-you-get-sore-after-sex/life-style/health-fitness/health-news/5-reasons-you-get-sore-after-sex/photostory/61101724.cms [date] => Mon, 16 Oct 2017 10:21:27 GMT)... 
関連する問題