定數組某臺下面是我的,我想按類別分開新聞的問題,我有以下的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函數來獲取一組具有某一類沒有運氣消息試過。
你對此有什麼建議?或者如果你能給我提供一個很棒的例子。
在此先感謝!
這是實際的XML?你是從一些你無法控制的外部資源拉出來的,或者你是否真的把它存儲在一個文件中?如果是前者,爲什麼不使用數據庫? – NullUserException
爲什麼你不能使用數據庫? –
事實上,它是一個XML文件,可以用作導入程序,我可以從我無法控制的外部源獲取它。 –