2012-10-12 40 views
3

定數組某臺下面是我的,我想按類別分開新聞的問題,我有以下的txt文件(包括所有的消息通過我如何忽略使用PHP

<item></item> 

下面是一組4個新聞,我實際的文件我有成千上萬

<item> 
Title: News from Washington 
Author: John Doe 
Category: New Laws 
Body: News content... 
</item> 

<item> 
Title: News from Texas 
Author: General Lee 
Category: Road Accidents 
Body: News content/ 
</item> 

<item> 
Title: News from Georgia 
Author: Marcus Smith 
Category: Street Food 
Body: News content 
</item> 

<item> 
Title: News from Illinois 
Author: Robert Simpson 
Category: School Projects 
Body: News content 
</item> 

我有以下代碼:。

//I get the content from the news file: 
$news = file_get_contents("news.txt"); 

//Then I create the following variables to get each set of news from the news variable: 
$regexp = '@<item>(.*?)</item>@msi'; 

我想從這裏做的是,如果我只是想獲得一個新聞文件,其中包括只是「街頭食品」作爲一個類別,並消除/忽略其他類別的其他新聞的其餘部分。

例如

從上面的例子我的結果將是一個文件,該文件只包含這個項目:

<item> 
Title: News from Georgia 
Author: Marcus Smith 
Category: Street Food 
Body: News content 
</item> 

我使用preg_match_all和一個foreach函數來獲取一組具有某一類沒有運氣消息試過。

你對此有什麼建議?或者如果你能給我提供一個很棒的例子。

在此先感謝!

+0

這是實際的XML?你是從一些你無法控制的外部資源拉出來的,或者你是否真的把它存儲在一個文件中?如果是前者,爲什麼不使用數據庫? – NullUserException

+0

爲什麼你不能使用數據庫? –

+0

事實上,它是一個XML文件,可以用作導入程序,我可以從我無法控制的外部源獲取它。 –

回答

3

您可以使用以下

#Rewrite the array to new XML Fromat 
rewriteToXML($final,"log.xml"); 

這種嘗試

$final = array(); 
$filename = "log.txt"; 
$news = simplexml_load_file($filename); 

foreach ($news as $item) { 
    $item = trim($item); 
    $content = array(); 
    foreach (explode("\n", $item) as $info) { 
     list($title, $data) = explode(":", $info); 
     $content[trim($title)] = $data; 
    } 
    $final[trim($content['Category'])][] = $content; 
} 


#Remove Street Food 
unset($final['Street Food']); 

#Output The Rest 
var_dump($final); 

輸出

array 
    'New Laws' => 
    array 
     0 => 
     array 
      'Title' => string ' News from Washington' (length=21) 
      'Author' => string ' John Doe' (length=9) 
      'Category' => string ' New Laws' (length=9) 
      'Body' => string ' News content...' (length=16) 
    'Road Accidents' => 
    array 
     0 => 
     array 
      'Title' => string ' News from Texas' (length=16) 
      'Author' => string ' General Lee' (length=12) 
      'Category' => string ' Road Accidents' (length=15) 
      'Body' => string ' News content/' (length=14) 
    'School Projects' => 
    array 
     0 => 
     array 
      'Title' => string ' News from Illinois' (length=19) 
      'Author' => string ' Robert Simpson' (length=15) 
      'Category' => string ' School Projects' (length=16) 
      'Body' => string ' News content' (length=13) 

您還可以Rewrite The XML將返回

<?xml version="1.0"?> 
<items> 
    <item> 
     <Title> News from Washington</Title> 
     <Author> John Doe</Author> 
     <Category> New Laws</Category> 
     <Body> News content...</Body> 
    </item> 
    <item> 
     <Title> News from Texas</Title> 
     <Author> General Lee</Author> 
     <Category> Road Accidents</Category> 
     <Body> News content/</Body> 
    </item> 
    <item> 
     <Title> News from Illinois</Title> 
     <Author> Robert Simpson</Author> 
     <Category> School Projects</Category> 
     <Body> News content</Body> 
    </item> 
</items> 

讀取新的格式更容易

$final = array(); 
$filename = "log.xml"; 
$news = simplexml_load_file($filename); 

foreach ($news as $item) { 
    #Check if not Street Food 
    if(trim($item->Category) != 'Street Food') 
      $final[trim($item->Category)][] = (array) $item; 
} 

#Output The Rest 
var_dump($final); 

重新寫入功能

function rewriteToXML($array, $fileName = null) { 
    $xml = new SimpleXMLElement("<items />"); 
    foreach ($array as $key => $item) { 
     $child = $xml->addChild("item"); 
     foreach ($item as $list) { 
      foreach ($list as $title => $data) 
      { 
       $child->addChild($title, $data); 
      } 
     } 
    } 
    $xml->asXML($fileName); 
} 
+0

酷巴巴!第二個選項看起來很完美,因爲@Joshua提到simpleXML是實現這一目標的最佳方式,我會嘗試一下,看看它是否可行!,brb :) –

+1

即使您使用'simplexml_load_file',將項目的函數編寫爲SimpleXMLElement ....你可以使用'rewriteToXML'將你的當前格式轉換爲另一種格式... – Baba

0

如果這是一個xml文件,我會使用simpleXML而不是正則表達式。然後你可以使用xQuery查詢simpleXML文檔。

http://php.net/manual/en/book.simplexml.php

+1

不確定爲什麼這會被標記下來。您可以將上面給出的例子加載到一個simpleXML dom文檔中,然後生成一個xPath來查找您要查找的內容。 –

+0

有趣的是,我會閱讀它的文檔,謝謝! –