2016-11-16 73 views
1

我有一個RSS源的遊戲。什麼是最好的方式來提取然後修改和回顯所選標籤的文本只從每個項目?PHP獲取並修改某些RSS標籤的字符串

因此,例如文本輸出我想應該是:

Game: Rapunzel Split Up With Flynn (HTML5) 
Category: girl 
Image: http://www.website.com/thumb/201611/Rapunzel-Split-Up-With-Flynn.jpg 
Game: Barbie Clean Place (HTML5) 
Category: girl 
Image: http://www.website.com/thumb/201611/Barbie-Clean-Place.jpg 

有更多的項目但正如我剛纔顯示出許多的2。

<?xml version="1.0" encoding="utf-8" ?> 
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:atom="http://www.w3.org/2005/Atom"> 
<channel> 
<title>Game Feed</title> 
<link>http://www.website.com</link> 
<description>Game Feed</description> 
<language>en-us</language> 
<atom:link href="http://website.com" rel="self" type="application/rss+xml" /> 
<item> 
<id>18859</id> 
<name>Rapunzel Split Up With Flynn</name> 
<type>html5</type> 
<description>Rapunzel wants to split up with Flynn, but what the result will be, it depends on you. Can you help her find her true love during this hard period?</description> 
<control>Tap on screen on mobile phone and mouse click on PC.</control> 
<tags>Princess, Movie, Love, HTML5, Hidden, Girl, Disney, Cartoon, Android</tags> 
<category>girl</category> 
<thumb>http://www.website.com/thumb/201611/Rapunzel-Split-Up-With-Flynn.jpg</thumb> 
<filetype>iframe</filetype> 
<file>http://website.com/Rapunzel-Split-Up-With-Flynn/index.php?pubid=website</file> 
<width>800</width> 
<height>504</height> 
<size>8454545</size> 
<ad>1</ad> 
<copyright>1</copyright> 
<resize>1</resize> 
<wmode_direct>0</wmode_direct> 
<mobile>1</mobile> 
<m_width>800</m_width> 
<m_height>504</m_height> 
<site>website.com</site> 
<publishDate>2016-11-15</publishDate> 
</item> 
<item> 
<id>18858</id> 
<name>Barbie Clean Place</name> 
<type>html5</type> 
<description>Little Barbie moved to a new home, following her dad's work transfer. As a new friend, would you like to help her and build a new home for her? Very simple, clean the messy room, and then decorate it, come and try it!</description> 
<control>Tap and drag to play on mobile phone and mouse click to play on PC.</control> 
<tags>Room, Princess, HTML5, Girl, Educational, Design, Decorate, Cleaning, Cartoon, Barbie, Android</tags> 
<category>girl</category> 
<thumb>http://www.website.com/thumb/201611/Barbie-Clean-Place.jpg</thumb> 
<filetype>iframe</filetype> 
<file>http://website.com/Barbie-Clean-Place/index.php?pubid=website</file> 
<width>800</width> 
<height>504</height> 
<size>4130080</size> 
<ad>1</ad> 
<copyright>1</copyright> 
<resize>1</resize> 
<wmode_direct>0</wmode_direct> 
<mobile>1</mobile> 
<m_width>800</m_width> 
<m_height>504</m_height> 
<site>website.com</site> 
<publishDate>2016-11-15</publishDate> 
</item> 
</channel> 
</rss> 

有什麼好的RSS或XML處理庫我可以使用或一些簡單的代碼?

+1

我用[SimpleXML的(http://php.net/manual/en/book.simplexml.php)的RSS源,工作得很好。 – simon

回答

1

把你的XML和JSON做或數組這樣你就可以訪問它更容易

$xml = '<xmlstuff></xmlstuff'; // Be warned of ' xml could cause error 

$json = json_encode(simplexml_load_string($xml), true); 
$arrays = json_decode($json, true); 

echo "<pre>"; 
var_dump($arrays); 
echo "</pre>"; 

現在,您可以訪問諸如陣列中的數據!

echo $name1 = $arrays['channel']['item'][0]['name']."<br>"; 
echo $name2 = $arrays['channel']['item'][1]['name']."<br><br>"; 

//MORE ADVANCED 

foreach ($arrays['channel']['item'] as $items){ 
    echo $items['name']."<br>"; 
} 

我在這裏爲你創建了一個頁面,這樣你就可以看到它是如何轉換和使用的!

http://fullertoncomputerepairwebdesign.com/tools/stackexchange/xmlparse.php

+0

這很有用,謝謝 – zeddex

+0

Off topic我已經接受了答案,但是你知道如何在不轉換爲json的情況下運行foreach循環,所以常規的簡單xml方式? – zeddex

+1

http://www.w3schools.com/php/func_simplexml_load_string.asp將是很好的匹配,其相同的概念屁數組,但更多的基於對象,所以你的使用 - > –