preg_match issues
Posted: Mon Dec 15, 2008 10:39 pm
I'm attempting to parse what is called a "duration" value in icalendar. It is basically a duration of time in weeks, days, hours, minutes, and seconds. A duration value typically looks like this: "P1W3DT2H3M45S" (for 1 week, 3 days, 2 hours, 3 minutes, and 45 seconds), but it can also look like this "PT2H" (2 hours). I wrote a regex to pull this information out, but it doesn't seem to want to work in some cases 
If you pass in "P1W3DT2H3M45S", $matches looks something like this:
But PT2H gives me this
Code: Select all
preg_match('/^[+-]?P([0-9]{1,2}[W])?([0-9]{1,2}[D])?T?([0-9]{1,2}[H])?([0-9]{1,2}[M])?([0-9]{1,2}[S])?$/i', $value, $matches);Code: Select all
array(6) {
[0]=>
string(13) "P1W3DT2H3M45S"
[1]=>
string(2) "1W"
[2]=>
string(2) "3D"
[3]=>
string(2) "2H"
[4]=>
string(2) "3M"
[5]=>
string(3) "45S"
}
Code: Select all
array(4) {
[0]=>
string(4) "PT2H"
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(2) "2H"
}