2012-03-11 115 views
2

任何人都可以推薦一個腳本/一些代碼來獲取iCalendar日曆文件並以純文本形式輸出當天的事件嗎?PHP:以純文本形式輸出iCalendar文件

目前代碼

<?php 
/** 
* This example demonstrates how the Ics-Parser should be used. 
* 
* PHP Version 5 
* 
* @category Example 
* @package Ics-parser 
* @author Martin Thoma <[email protected]> 
* @license http://www.opensource.org/licenses/mit-license.php MIT License 
* @version SVN: <svn_id> 
* @link  http://code.google.com/p/ics-parser/ 
* @example $ical = new ical('MyCal.ics'); 
*   print_r($ical->get_event_array()); 
*/ 
require 'class.iCalReader.php'; 

$ical = new ICal('THE URL'); 
$events = $ical->events(); 

$date = $events[0]['DTSTART']; 
foreach ($events as $event) { 
    echo "".$event['SUMMARY']."<br/>"; 
} 
?> 

<style> 
body 
{ 
    font-family: Segan; 
} 
</style> 

回答

2

我建議ICS-Parser

將ICS轉換爲數組可以很好地完成工作,您可以循環使用並打印您喜歡的數據,例如在他們的網站上。

+0

如何下載它,因爲下載(http://code.google.com/p/ics -parser/downloads/list)它只是說'這個項目目前沒有下載。'? – 2012-03-11 12:34:07

+0

點擊此處的文件:http://code.google.com/p/ics-parser/source/browse/#svn%2Ftrunk - 然後選擇'查看原始文件' – 472084 2012-03-11 12:55:24

+1

謝謝。我有它的工作,它看起來非常好,但我怎麼能改變它只顯示它被訪問的一天(今天)發生的事情? (我把我已經得到的問題,因爲它太大,不能放在這裏) – 2012-03-11 13:05:17

3

你需要讀或寫? 讀我在過去使用:

http://sevengoslings.net/icalendar

http://www.phpclasses.org/browse/file/16660.html

http://sourceforge.net/projects/phpicalendar/ - <我相信這其中也可以讀,但它是巨大的 - 你可能只需要一個功能或從那裏兩個

但我明白你的問題,你需要閱讀 - iCalender是純文本。 您只需要打開文件並逐行繪製。

<?php 
$data = file_get_contents("myfile.ics"); //read the file 
$convert = explode("\n", $data); //create array separate by new line 

for ($i=0;$i<count($convert);$i++) 
{ 
    echo $convert[$i].', '; //write value by index 
} 
?> 

,然後使用正則表達式或別的東西,給人類readeble格式標籤..

編輯我:

我剛剛發現我使用的函數之前:它使用 這個課程:http://www.kigkonsult.se/iCalcreator/index.php 這不是我寫的,但它在過去對我有效。 我不記得這個功能的源 - 如果我會找到它我會後..

<?php 

      require_once 'iCalcreator/iCalcreator.class.php'; 

       $filename = 'D:\Document\Docs\2007\05\iCal-20070508-082112.ics'; 

       $v = new vcalendar(); // initiate new CALENDAR 
       $v->parse($filename); 

       # get first vevent 
       $comp = $v->getComponent("VEVENT"); 

       #print_r($comp); 
       $summary_array = $comp->getProperty("summary", 1, TRUE); 
       echo "summary: ", $summary_array["value"], "\n"; 

       $dtstart_array = $comp->getProperty("dtstart", 1, TRUE); 
       $dtstart = $dtstart_array["value"]; 
       $startDate = "{$dtstart["year"]}-{$dtstart["month"]}-{$dtstart["day"]}"; 
       $startTime = "{$dtstart["hour"]}:{$dtstart["min"]}:{$dtstart["sec"]}"; 

       $dtend_array = $comp->getProperty("dtend", 1, TRUE); 
       $dtend = $dtend_array["value"]; 
       $endDate = "{$dtend["year"]}-{$dtend["month"]}-{$dtend["day"]}"; 
       $endTime = "{$dtend["hour"]}:{$dtend["min"]}:{$dtend["sec"]}"; 

       echo "start: ", $startDate,"T",$startTime, "\n"; 
       echo "end: ", $endDate,"T",$endTime, "\n"; 

      ?>