如何从PHP Regex中的Facebook Events解析Ical文件? [英] How to parse Ical file from Facebook Events in PHP Regex?

查看:56
本文介绍了如何从PHP Regex中的Facebook Events解析Ical文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析此数据中的 Summary DTSTART 字段,并考虑使用正则表达式.还尝试逐行阅读,但是无法解决实现它的逻辑.

I am trying to parse the Summary and DTSTART fields in this data and thought about using regex. Also tried reading line by line but couldn't work around the logic to implement it.

有人帮忙吗?

已经有解析器了,但是我的要求有点独特,并且需要不同的目标实现.

There are already made parsers out there but my requirements are abit unique and require a different targeted implementation.

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()),然后一次处理每一行.每行都分为标签和内容部分,然后根据标签是什么,它将临时存储数据或将累积的内容添加到整个日历数组中....

This code reads all of the file into an array (using file()) and then processes each line at a time. Each line is split into the tag and the content parts and then depending on what tag it is, it will either store the data temporarily or add the accumulated content into the overall calendar array. ...

$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标记内容将文件各个部分的事件与示例数据文件一起存储...

It uses the BEGIN tag content to store the events of the various parts of the file together, with the sample data file it will give...

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
                )

        )

)

这篇关于如何从PHP Regex中的Facebook Events解析Ical文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆