2012-03-07 75 views
1

我有一個編輯腳本,它檢索日曆事件並讓用戶編輯這些事件。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()函數確實有效。這只是一個星期的功能,給出了錯誤。

任何幫助表示讚賞,謝謝:)

回答

2

我看到你的第二個模式的問題。您正在檢查BYDAY之後的數字,但您的示例在該位置有字母字符。這會導致第二種模式中只有4個匹配項,而您嘗試訪問[5]時會導致未定義的偏移量。

DTSTART;VALUE=DATE:20120201 DTEND;VALUE=DATE:20120201 RRULE:FREQ=WEEKLY;BYDAY=Tu,Fr;UNTIL=20120331 

/^DTSTART;VALUE=DATE:(\d+)\s+DTEND;VALUE=DATE:(\d+)\sRRULE:FREQ=(\w+);BYDAY=(\d+);UNTIL=(\d+)/ 
//-------------------------------------------------------------------------^^^^^^^ 

相反,你可以嘗試更換與[A-za-z,]匹配字母字符和逗號。

/^DTSTART;VALUE=DATE:(\d+)\s+DTEND;VALUE=DATE:(\d+)\sRRULE:FREQ=(\w+);BYDAY=([A-za-z,]+);UNTIL=(\d+)/ 
//---------------------------------------------------------------------------^^^^^^^ 
1

在每週的正則表達式BYDAY=(\d+)不正確,\ d表示數字,這意味着它不會匹配字符串「莫神父」。改爲Try BYDAY=([\w,]+)

+0

說起來,書籤RegExr是調試正則表達式的好工具。 http://gskinner.com/RegExr/ – Janek 2012-03-07 14:10:22