2017-04-17 34 views
-1

使用這個RSS種子http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml如何解析RSS提要中的命名空間條目?

我如何得到<media:credit>Pete Kiehart for The New York Times</media:credit>只輸出「Pete Kiehart」?

所以我得到了這個。這是一個獲取圖像和文本的RSS XML設備。所以這樣的:

RSS Code from NYT

成爲該

Output

代碼做的是這樣的:

<?php 
    $url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data 
    $feeds = file_get_contents($url); 
    $rss = simplexml_load_string($feeds); 

    $items = []; 

    foreach($rss->channel->item as $entry) { 
     $image = ''; 
     $image = 'N/A'; 
     foreach ($entry->children('media', true) as $k => $v) { 
      $attributes = $v->attributes(); 

      if (count($attributes) == 0) { 
       continue; 
      } else { 
       $image = $attributes->url; 
      } 
     $content_data = (string)$entry->children("media", true)->description; 
     $credit = (string)$entry->children("media", true)->credit; 
     } 
     $items[] = [ 
      'link' => $entry->link, 
      'title' => $entry->title, 
      'image' => $image, 
      'Desc' =>$content_data, 
      'credit' =>$credit, 
     ]; 
    } 
$i=0; 
foreach ($items as $item) { 
if ($i < 3) { 

    printf('<img src="%s">', $item['image']); 
    printf('<a href="%s">%s</a>', $item['link'], $item['title']); 
    printf('<p>%s</p>', $item['Desc']); 
    printf('<p>credit to: %s</p>', $item['credit']); 
    $i++; 
    } 
    } 
    ?> 

我想接下來做的就是轉變信用信息逼到名字。所以:

printf('<p>credit to: %s</p>', $item['credit']); 

其輸出[功勞:皮特Kiehart爲紐約時報]這 變爲[信用:皮特Kiehart。

我該怎麼做這個或那個?

編輯:

的一種方式做到這一點作爲也由friend at Discord從字符串獲得兩個詞提及。

的代碼是

<?php 

$message="AB 1234 Hello, how are you?"; 
print_r(preg_split('/[\s,]+/', $message, 3)); 

我試圖用那一個,但我不能似乎將它放入自己的代碼。使其在信用信息上工作。

編輯2:好的我到了某個地方。

使用:

$credit = preg_split('/[\s,]+/', $credit);

在$信貸=(字符串foreach循環..

print_r(array_values($item['credit']));

,而不是

printf('<p>credit to: %s</p>', $item['credit']);

這是迄今爲止的結果:

going forward

編輯3:獲取更近。

New

使用代碼:

print_r(array_values($item['credit'])); 

    ?> <br><b>New output!: </b> <?php 

    echo implode(" ",$item['credit']); ?> 
<br><br> <?php 
    $i++; 
    } 
    } ?> 
+0

我看着這一點 - > Stackflow關於 '獲得前兩個字從PHP字符串' @ HTTP: //stackoverflow.com/questions/13263523/getting-first-two-words-from-string-in-php - 但我似乎不能夠「樂高」的功能正常到我的創作。 – xxxx

+0

[從PHP字符串獲得頭兩個字]的可能的複製(http://stackoverflow.com/questions/13263523/getting-first-two-words-from-string-in-php) – Trasiva

回答

0

事情是這樣的:

<?php 
$url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data 
$feeds = file_get_contents($url); 
$rss = simplexml_load_string($feeds); 

$items = []; 

foreach($rss->channel->item as $entry) { 
    $image = ''; 
    $image = 'N/A'; 
    $description = 'N/A'; 
    $credit = 'N/A'; 
    foreach ($entry->children('media', true) as $k => $v) { 
     $attributes = $v->attributes(); 

     if ($k == 'content') { 
      if (property_exists($attributes, 'url')) { 
       $image = $attributes->url; 
      } 
     } 
     if ($k == 'description') { 
      $description = $v; 
     } 
     if ($k == 'credit') { 
      $parts = explode('for', $v); 
      if (count($parts) > 1) { 
       $credit = $parts[0]; 
      } else { 
       $credit = $v; 
      } 
     } 
    } 

    $items[] = [ 
     'link' => $entry->link, 
     'title' => $entry->title, 
     'image' => $image, 
     'Desc' => $description, 
     'credit' => $credit, 
    ]; 
} 

$i=0; 
foreach ($items as $item) { 
    if ($i < 3) { 
     printf('<img src="%s">', $item['image']); 
     printf('<a href="%s">%s</a>', $item['link'], $item['title']); 
     printf('<p>%s</p>', $item['Desc']); 
     printf('<p>credit to: %s</p>', $item['credit']); 
     $i++; 
    } 
} 

?> 
+0

東西是很好的。但是你能否提供一種格式化代碼的方法,以便只顯示名稱而不是'array'[0] [1]不顯示。 – xxxx

+1

將代碼添加到您的循環中 – alistaircol

+0

已添加。得到同樣的結果。 「信用:皮特Kiehart爲紐約時報」 – xxxx