I have an edit script which retrieves calendar events and lets the user edit these events.
There are 3 types of events; single occurring, daily recurring, and weekly recurring.
The edit script handles all 3 types (where the format of the data is slightly different for each).
Regarding the 2 types of recurring events, there is no way for me to tell which one is being processed until I use preg_match. I have 2 preg_match functions, the first is for daily recurring events, and the second is for weekly recurring events.
My code first passes the string which contains the event info into the daily preg_match function. If the variables are assigned then there is no problem.
If the variables however have not been assigned, I then pass the same string into the weekly preg_match function. This is where I get following error:
Notice: Undefined offset:
I will now show my code.
To begin with, the 2 types of string:
(Daily Recurring Event)
DTSTART;VALUE=DATE:20120306 DTEND;VALUE=DATE:20120307 RRULE:FREQ=DAILY;INTERVAL=3;UNTIL=20120331
(Weekly Recurring Event)
DTSTART;VALUE=DATE:20120201 DTEND;VALUE=DATE:20120201 RRULE:FREQ=WEEKLY;BYDAY=Tu,Fr;UNTIL=20120331
The above strings are stored in the variable $eventtype
The code to process this:
recurrence_info_day($eventtype);
$recurrence_type = "daily";
if ($d = false){
recurrence_info_weekly($eventtype);
$recurrence_type = "weekly";
}
function recurrence_info_day($eventtype){
global $eventstart, $eventend, $eventfrequency, $eventinterval, $eventuntil, $formstartdate, $formenddate, $xyz;
$s = $eventtype;
preg_match(
'/^DTSTART;VALUE=DATE:(\d+)\s+DTEND;VALUE=DATE:(\d+)\s+RRULE:FREQ=(\w+);INTERVAL=(\d+);UNTIL=(\d+)/',
$s,
$recinfod
);
$eventstart = $recinfod[1];
$eventend = $recinfod[2];
$eventfrequency = $recinfod[3];
$eventinterval = $recinfod[4];
$eventuntil = $recinfod[5];
$formstartdate = substr($eventstart,4,2)."/".substr($eventstart, 6)."/".substr($eventstart,0,4);
$formenddate = substr($eventuntil,4,2)."/".substr($eventuntil, 6)."/".substr($eventuntil,0,4);
$d = true;
if (!$eventstart){
$d = false;
}
}
//Weekly recurring events
function recurrence_info_weekly($eventtype){
global $eventstart, $eventend, $eventfrequency, $eventdays, $eventuntil, $formstartdate, $formenddate;
$s = $eventtype;
preg_match(
'/^DTSTART;VALUE=DATE:(\d+)\s+DTEND;VALUE=DATE:(\d+)\sRRULE:FREQ=(\w+);BYDAY= (\d+);UNTIL=(\d+)/',
$s,
$recinfow
);
$eventstart = $recinfow[1];
$eventend = $recinfow[2];
$eventfrequency = $recinfow[3];
$eventdays = $recinfow[4];
$eventuntil = $recinfow[5];
$formstartdate = substr($eventstart,4,2)."/".substr($eventstart, 6)."/".substr($eventstart,0,4);
$formenddate = substr($eventuntil,4,2)."/".substr($eventuntil, 6)."/".substr($eventuntil,0,4);
}
When I have a daily recurring event, this script works as it should.
From what I can tell, the problem is with the preg_match function for the weekly string – recurring_info_weekly()
I get undefined offset errors when I call this function alone, or as in the above code.
The recurring_info_day() function does work. It’s just the week function that gives the error.
Any help is appreciated, thank you 🙂
I see a problem in your second pattern. You are checking for digits following
BYDAY, but your example has alpha characters in that position. This would cause only 4 matches in the second pattern, and the undefined offset when you attempt to access[5].Instead you may try replacing that with
[A-za-z,]to match alpha characters and the comma.