解析器(ICS / PHP)仅解析第一行

时间:2017-10-05 16:00:04

标签: php parsing icalendar

我有一个ics-parser:

 <?php
class ics {
    /* Function is to get all the contents from ics and explode all the datas according to the events and its sections */
    function getIcsEventsAsArray($file) {
        $icalString = file_get_contents ( $file );
        $icsDates = array ();
        /* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
        $icsData = explode ( "BEGIN:", $icalString );
        /* Iterating the icsData value to make all the start end dates as sub array */
        foreach ( $icsData as $key => $value ) {
            $icsDatesMeta [$key] = explode ( "\n", $value );
        }
        /* Itearting the Ics Meta Value */
        foreach ( $icsDatesMeta as $key => $value ) {
            foreach ( $value as $subKey => $subValue ) {
                /* to get ics events in proper order */
                $icsDates = $this->getICSDates ( $key, $subKey, $subValue, $icsDates );
            }
        }
        return $icsDates;
    }

    /* funcion is to avaid the elements wich is not having the proper start, end  and summary informations */
    function getICSDates($key, $subKey, $subValue, $icsDates) {
        if ($key != 0 && $subKey == 0) {
            $icsDates [$key] ["BEGIN"] = $subValue;
        } else {
            $subValueArr = explode ( ":", $subValue, 2 );
            if (isset ( $subValueArr [1] )) {
                $icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
            }
        }
        return $icsDates;
    }
}


$obj = new ics();
$icsEvents = $obj->getIcsEventsAsArray( 'calender.ics' );

echo "<h1>2017</h1>";

if($icsEvents)
{
    $timeZone = date_default_timezone_get() ;
    unset($icsEvents[1]);
    foreach ($icsEvents as $key => $icsEvent)
    {
        $start = isset( $icsEvent ['DTSTART;VALUE=DATE'] ) ? $icsEvent ['DTSTART;VALUE=DATE'] : $icsEvent ['DTSTART'];
        $startDt = new DateTime ( $start );
        $startDt->setTimeZone ( new DateTimezone ( $timeZone ) );
        $startDate = $startDt->format ( 'm/d/Y h:i' );

        $eventMonth = $startDt->format ( 'M Y' );
        ?>
        <h2><?php echo $eventMonth; ?></h2>
        <h3><?php echo $icsEvent['SUMMARY']; ?></h3>
        <?php echo $startDate; ?>, <?php echo $icsEvent['DESCRIPTION']; ?>
        <?php
    }
}



?>

此解析器接受一个ics-file并将事件从ics-file放入一个html页面。

我的ics-file看起来像这样:

BEGIN:VCALENDAR
METHOD:PUBLISH
PRODID:-//John Papaioannou/NONSGML Bennu 0.1//EN
VERSION:2.0
BEGIN:VEVENT
UID:249@moo.banma.fileserver.com/moo
SUMMARY:Greet
DESCRIPTION:
CLASS:PUBLIC
LAST-MODIFIED:20170906T131259Z
DTSTAMP:20170926T093038Z
DTSTART:20170908T100000Z
DTEND:20170908T130000Z
CATEGORIES:Allgemeine Termine
END:VEVENT
BEGIN:VEVENT
UID:420@moo.banma.fileserver.com/moo
SUMMARY:Make PC run again
DESCRIPTION:
CLASS:PUBLIC
LAST-MODIFIED:20170906T131300Z
DTSTAMP:20170926T093038Z
DTSTART:20170908T130000Z
DTEND:20170908T140000Z

有时,描述/摘要有多行:

SUMMARY:Make PC run again
and show it to big boss

我如何更改我的解析器代码,它显示所有摘要文本和描述文本,即使它是多行/即使它内部有换行符?

谢谢!

0 个答案:

没有答案