我有一个编辑脚本,它检索日历事件并让用户编辑这些事件。PHP preg_match/undefined偏移量
有3种类型的事件;单发,每日复发和每周复发。
编辑脚本处理所有3种类型(其中数据格式各不相同)。
关于这两种类型的重复性事件,在我使用preg_match之前,我无法分辨哪一个正在处理。我有2个preg_match函数,第一个是每日循环事件,第二个是每周循环事件。
我的代码首先将包含事件信息的字符串传递给每日preg_match函数。如果变量被赋值,那么没有问题。
如果变量没有被赋值,我然后将相同的字符串传入每周preg_match函数。这是我得到以下错误:
Notice: Undefined offset:
我现在将显示我的代码。
首先,在2种类型的串的:
(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
上述字符串存储在变量$的EventType
的代码来处理该:
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);
}
当我有一个每日循环事件,这个脚本正常工作。
从我可以告诉,问题是每周字符串的preg_match函数 - recurring_info_weekly() 当我单独调用此函数或上述代码时,出现未定义的偏移错误。 recurring_info_day()函数确实有效。这只是一个星期的功能,给出了错误。
任何帮助表示赞赏,谢谢:)
说起来,书签RegExr是调试正则表达式的好工具。 http://gskinner.com/RegExr/ – Janek 2012-03-07 14:10:22