这是我遇到的问题,我正在尝试按类别分隔新闻,我有以下txt文件(其中包括所有新闻除以
<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';
我想从这里做的是,如果我只想获得一个包含新闻的文件,其中仅包含“街头食品”作为一个类别,并且忽略/忽略具有不同类别的其他新闻。
e.g。
上述示例的结果将是一个仅包含此项目的文件:
<item>
Title: News from Georgia
Author: Marcus Smith
Category: Street Food
Body: News content
</item>
我尝试使用preg_match_all和foreach函数来获取一组具有特定类别但没有运气的新闻。
你有什么建议来实现这个目标?或者如果你能给我一个很棒的例子。
提前致谢!
答案 0 :(得分:3)
你可以尝试
$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
使用以下
#Rewrite the array to new XML Fromat
rewriteToXML($final,"log.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);
}
答案 1 :(得分:0)
如果这是一个xml文件,我会使用simpleXML而不是正则表达式。然后,您可以使用xQuery查询simpleXML文档。