[英]reading a directory of xml files with php
我有这段代码可以找到目录中的每个xml文件,然后获取日期。 但是我希望它代替在目录中每个xml文件中的每一行。 xml文件如下所示
<event>
<day>11</day>
<month>01</month>
<year>2013</year>
<link>link</link>
<title>Title</title>
<description></description>
</event>
和我的PHP代码
$events = array();
// open each xml file in this directory
foreach(glob("*.xml") as $filename) {
// get the contents of the the current file
$xml_file = file_get_contents($filename, FILE_TEXT);
// create a simplexml object from the contents of the current file
$xml = simplexml_load_string($xml_file);
// create a day, link, description, etc.
$eventDay = (int)$xml->day;
$eventMonth = (int)$xml->month;
$eventYear = (int)$xml->year;
$eventLink = (string)$xml->link;
$eventDesc = (string)$xml->description;
if($eventMonth == $month && $eventYear == $year)
$events[$eventDay] = array($eventLink,'linked-day');
}
return $events;
}
因此,例如,对于找到的每个文件,它都会打印出类似
<div class="event"><p>Date: month/day/year</p><p>Title: the xml title</p><p>description: the xml description</p></div>
作为一种简单的解决方案,如果您想将事件作为循环的一部分回显,则可以执行以下操作:
$events = array();
// open each xml file in this directory
foreach(glob("*.xml") as $filename) {
// get the contents of the the current file
$xml_file = file_get_contents($filename, FILE_TEXT);
// create a simplexml object from the contents of the current file
$xml = simplexml_load_string($xml_file);
// create a day, link, description, etc.
$eventDay = (int)$xml->day;
$eventMonth = (int)$xml->month;
$eventYear = (int)$xml->year;
$eventLink = (string)$xml->link;
$eventDesc = (string)$xml->description;
$eventTitle = (string)$xml->title;
if($eventMonth == $month && $eventYear == $year)
$events[$eventDay] = array($eventLink,'linked-day');
echo "<div class=\"event\">
<p>Date: {$eventMonth}/{$eventDay}/{$eventYear}</p>
<p>Title: {$eventTitle}</p>
<p>description: {$eventDesc}</p>
</div>";
}
return $events;
唯一的增加是$eventTitle
和echo
部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.