Hi,
I've been struggling with a tiny little issue (being able to parse an iCal (.ics) file using PHP,) for the past two days. Within that time, I did happen to find a good-looking project called libical that provides some classes for parsing an iCal file with PHP.
I knew there surely had to be a simple way to parse an .ics file, because it's just a text file with a Key/Value type of structure! But I couldn't figure out what type of regular expressions to use, etc.
After searching around here on the PHPBuilder board, and after coming across this wayward post (http://phpbuilder.com/board/showthread.php?t=10285133) I realized how to make this work.
Here's what I did:
$the_file = file_get_contents('filename.ics');
$total_entries = array();
$data = array();
// First make sure to find any lines from the .ics that are broken up by a hard return, and append the broken line to the line above.
$the_file = preg_replace("'([\r\n])[\s]+'", '', $the_file);
// Then break up each line from the .ics file into the array $total_entries
$total_entries = preg_split("/[\r\n]+/", $the_file);
// Lastly, loop through all of the values in the $total_entries array
foreach ($total_entries as $line) {
list ($key, $value) = explode(":", $line, 2);
$data[trim($key)] = trim($value);
// ... you can use this line-by-line information however you need to
}
Of course, there are still a lot of things you can add to this to make it work better. At any rate, I hope this helps anyone who is looking for a simple way to parse an iCal (.ics) file with PHP.