How to parse Ical file from Facebook Events in PHP Regex?

人盡茶涼 提交于 2021-02-05 09:37:26

问题


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.

Anyone help out?

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

回答1:


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;
    }
}

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
                )

        )

)


来源:https://stackoverflow.com/questions/56459766/how-to-parse-ical-file-from-facebook-events-in-php-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!