我正在尝试解析此数据中的Summary
和DTSTART
字段,并考虑使用正则表达式。还尝试逐行阅读,但是无法解决实现它的逻辑。
有人帮忙吗?
已经有解析器了,但是我的要求有点独特,并且需要不同的目标实现。
BEGIN:VCALENDAR
PRODID:-//Facebook//NONSGML Facebook Events V1.0//EN
X-WR-CALNAME:Friends' birthdays
X-PUBLISHED-TTL:PT12H
X-ORIGINAL-URL:/events/birthdays/
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
DTSTART:20170106
SUMMARY:Gys's birthday
RRULE:FREQ=YEARLY
DURATION:P1D
UID:b1074083@facebook.com
END:VEVENT
BEGIN:VEVENT
DTSTART:20130406
SUMMARY:Geo's birthday
RRULE:FREQ=YEARLY
DURATION:P1D
UID:b1004@facebook.com
END:VEVENT
BEGIN:VEVENT
DTSTART:20120602
SUMMARY:Flo's birthday
RRULE:FREQ=YEARLY
DURATION:P1D
UID:b100895@facebook.com
END:VEVENT
答案 0 :(得分:1)
此代码将所有文件读入数组(使用file()
),然后一次处理每一行。每行都分为标签和内容部分,然后根据它是什么标签,它将临时存储数据或将累积的内容添加到整个日历数组中。 ...
$file = "a.txt";
$calendar = [];
$lines = file($file, FILE_IGNORE_NEW_LINES);
$temp = [];
$type = "";
foreach ( $lines as $line ) {
list($tag,$content) = explode(":", $line);
if ( $tag == "END" ) {
$calendar[$type][] = $temp;
$temp = [];
}
else if ( $tag == "BEGIN" ) {
// If already some content, then store it in calendar and reset
if ( count($temp) > 0 ) {
$calendar[$type][] = $temp;
$temp = [];
}
$type = $content;
}
else {
$temp[$tag] = $content;
}
}
它使用BEGIN标记内容将文件各个部分的事件与示例数据文件一起存储...
Array
(
[VCALENDAR] => Array
(
[0] => Array
(
[PRODID] => -//Facebook//NONSGML Facebook Events V1.0//EN
[X-WR-CALNAME] => Friends' birthdays
[X-PUBLISHED-TTL] => PT12H
[X-ORIGINAL-URL] => /events/birthdays/
[VERSION] => 2.0
[CALSCALE] => GREGORIAN
[METHOD] => PUBLISH
)
)
[VEVENT] => Array
(
[0] => Array
(
[DTSTART] => 20170106
[SUMMARY] => Gys's birthday
[RRULE] => FREQ=YEARLY
[DURATION] => P1D
[UID] => b1074083@facebook.com
)
[1] => Array
(
[DTSTART] => 20130406
[SUMMARY] => Geo's birthday
[RRULE] => FREQ=YEARLY
[DURATION] => P1D
[UID] => b1004@facebook.com
)
[2] => Array
(
[DTSTART] => 20120602
[SUMMARY] => Flo's birthday
[RRULE] => FREQ=YEARLY
[DURATION] => P1D
[UID] => b100895@facebook.com
)
)
)