我有一个字符串变量,命名$message
如下:
* Time Stamp: 2012-12-03 16:36:04
* Speed: 7 km/h
* Heading: 356 deg (N)
* Event ID: -48
* Event Desc: .Arrived at Inbound Receiving
* Event Value: -56
* Event Value Type: 0
然后我尝试使用以下代码从变量中提取事件和时间戳值:
$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;
}
if(strpos($key, 'Time Stamp') > -1) {
$time = $val;
}
}
这很好用,唯一的问题是返回的值是:
.Arrived at Arrived at Inbound Receiving =
2012-12-03 16:36:04 =
等号从何而来,如何将其与尾随空格一起删除?
我的预期结果是:
.Arrived at Arrived at Inbound Receiving
和
2012-12-03 16:36:04
这就是它在字符串变量中的显示方式。一如既往地感谢。