PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

iCalendar / vCard parser for PHP

Note: This article was originally published at Planet PHP on 24 November 2010.
Planet PHP

I've just finished an iCalendar vCard parser for PHP. It's done almost completely with a 'natural' simplexml-like interface, so it should (hopefully) be just as easy to parse, and also modify iCalendar / vCard objects (ics/vcf files).

To install using pear, run the following:

  1. pear channel-discover pear.sabredav.org
  2. pear install sabredav/Sabre_VObject-alpha

Or download from pear.sabredav.org.

For testing, I used this iCalendar file: icalendartest.ics.

To load in an object, you use the Reader class:

  1. // Link to the correct path if you manually dowloaded the package
  2. include 'Sabre/VObject/includes.php';
  3. A
  4. // Reading an object
  5. $calendar = Sabre_VObject_Reader::read(file_get_contents('icalendartest.ics'));

iCalendar objects consist of components (VEVENT, VTODO, VTIMEZONE, etc), properties (SUMMARY, DESCRIPTION, DTSTART, etc) and parameters, which are to properties what attributes are to elements in XML. To show a listing of all events in a calendar, this snippet would work:

  1. echo "There are ", count($calendar-vevent), " events in this calendar\n";
  2. A
  3. // Looping through events
  4. foreach($calendar-vevent as $event) {
  5. A
  6. echo (string)$event-dtstart, ": ", $event-summary, "\n";
  7. A
  8. }

You can easily modify properties:

  1. $calendar-vevent[0]-description = "It's a birthday party";

Creating new objects uses the following syntax:

  1. $todo = new Sabre_VObject_Component('vtodo');
  2. $todo-summary = 'Take out the dog'

Truncated by Planet PHP, read more at the original (another 2031 bytes)