2012-12-04 28 views
0

我試圖從電子郵件管道獲取詳細信息。管道將$ message變量返回給我,其中包含數據的分配。我希望能夠搜索字符串的特定值並返回下一個'x'字符數量。PHP,在字符串中找到字符串(變量)後返回一組字符數

爲例,我的變量$message包含以下字符串:

Arrived at Inbound Receiving occurred 

on M03-Actros 33.50 (1231) (MX1) (LT) 1022 on 2012-12-03 

16:36:04 



       * Driver ID: person, RT (1231) 

       * Vehicle Desc: M03-Actros 33.50 (1234) (MX1) (LT) 

       * Vehicle ID: 1022 

       * Time Stamp: 2012-12-03 16:36:04 

       * Latitude: S31 11.870' 

       * Longitude: E031 44.555' 

       * Speed: 7 km/h 

       * Heading: 356 deg (N) 

       * Event ID: -48 

       * Event Desc: .Arrived at Inbound Receiving 

       * Event Value: -56 

       * Event Value Type: 0 

然後我想篩選出的事件說明。因此請搜索$message以獲取字符串'Event Desc:',然後返回該行上的其餘數據。所以從上面的例子中,我想設置變量「$事件」到「在入境.Arrived接收」

我知道我必須使用

if (strstr($subject, 'Event Desc: ')) { 

} 

但我不知道的價值,如何在回報考慮到數據的長度可能有所不同,剩餘的行數據。

任何幫助一如既往的讚賞,謝謝。

回答

1

我的建議是:

$event = null; 
$lines = explode(PHP_EOL, $message); 
foreach($lines as $line) { 
    // skip empty lines 
    if(strlen($line) == 0) { 
    continue; 
    } 
    $tokens = explode(':', $line); 
    // tokens[0] contains the key , e.g. Event Value 
    // tokens[1]~[N] contains the value (where N is the number of pieces), e.g. -56 
    // stitch token 1 ~ N 
    $key = $tokens[0]; 
    unset($tokens[0]); 
    $val = implode(':', $tokens); 
    // do your extra logic here, e.g. set $event variable to value 
    if(strpos($key, 'Event Desc') > -1) { 
    $event = $val; 
    } 
} 

限制:你的數據不能包含:

+0

這不回答這個問題。 –

+0

謝謝Shivan及時回覆,感謝。你能解釋這一點,以幫助我理解嗎?那麼我如何將變量'$ event'設置爲'event descr'之後的行餘數的值?再次感謝。 – Smudger

+0

和我的數據肯定會包含':'。根據問題抽樣數據。再次感謝。 – Smudger

相關問題