在php中解析iCalendar字符串的最佳方法

在php中解析iCalendar字符串的最佳方法

问题描述:

I am trying to write a class that can parse an iCalendar file and am hitting some brick walls. Each line can be in the format:

PARAMETER[;PARAM_PROPERTY..]:VALUE[,VALUE2..]

It's pretty easy to parse with either a bunch of splits or regex's until you find out that values can have backticked commas, also they can be double quote marked which makes life hard. for example:

PARAMETER:"my , cool, value",value\,2,value3

In this example you are meant to pull out the three values:

  • my , cool value
  • value,2
  • value3

Which makes it a little more difficult.

Suggestions?

我正在尝试编写一个可以解析iCalendar文件的类,并且正在敲击一些砖墙。 每行可以采用以下格式: p>

  PARAMETER [; PARAM_PROPERTY ..]:VALUE [,VALUE2 ..] 
  code>  pre> 
 \  n 

很容易用一堆拆分或正则表达式解析,直到你发现值可以有反引号的逗号,它们也可以是双引号,这会让生活变得艰难。 例如: p>

  PARAMETER:“my,cool,value”,value \,2,value3 
  code>  pre> 
 
 

在这个例子中,你想要取出三个值: p>

  • my,cool value li>
  • value,2 li> \ n
  • value3 li> ul>

    这使得它更难一点。 p>

    建议? p>

Go through the file char by char and split the values manually, whenever you have a quotation mark you enter "quotation mode" where you won't split at commas and when the closing quotation mark comes you leave it.

For the backticked commas: If you read in a backslash you also read the next character and decide what to do with it then.

Of course that's not extremely efficient, but you can't use regular expressions for this. I mean you can, but since I believe that there also can be escaped quotation marks this is going to be really messy.

If you want to give it a try though:

  • let's start by matching a quotation mark followed by characters that are not: "[^"]*"
  • to overcome the problem of escaped characters you can use lookaheads (?<!\\)"[^"]*(?<!\\)"
  • now it will break if escaped quotation marks are in the value, maybe this works?(haven't tested it) (?<!\\)"[^"|(?<=\\)"]*(?<!\\)"

So you see it very fast get's messy, so I would suggest to you to read it in characterwise.

Have you tried pulling something out of http://phpicalendar.net/ ?

I had the same problems. I found it a bit hard to turn 'any' iCalendar file into a usable PHP object/array structure, so instead I've been trying to convert iCalendar to xCal.

This is my implementation:

http://code.google.com/p/sabredav/source/browse/branches/caldav/lib/Sabre/CalDAV/ICalendarToXML.php

I must say that this script is not fully tested, but it might be enough to get your started.

Is this the project you're thinking of? I'm the auther :) The first usable version (v0.1.0) should be ready in about a month. It is capable of working with about 85% of the iCalendar spec right now, but recurring events are really tough. I'm working on them right now. Once those are complete, the library will be fully capable of doing anything in the spec.

qCal Google Code Homepage

Enjoy!