如何在 PHP 正则表达式中解析来自 Facebook 事件的 Ical 文件?
How to parse Ical file from Facebook Events in PHP Regex?
我正在尝试解析此数据中的 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
此代码将所有文件读入一个数组(使用 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
)
)
)
我正在尝试解析此数据中的 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
此代码将所有文件读入一个数组(使用 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
)
)
)