我有一个日志文件,其中包含Blocks中的数据。示例如下。正如您在每个区块recv_time
中看到的那样,Span
,From-Number
,Timestamp
,Type
,SMS-SMSC-Number
和Content
会重复出现。我想在php中获取它们的值并保存在数据库表中。每个块都有自己的值。我们将非常感谢您的帮助。谢谢。
----SMS_START----
recv_time:2014-10-09 18:32:39
Span: 1
From-Number: +1347XXXXXXX
Timestamp: 14/10/09 18:32:16 96
Type: PDU
SMS-SMSC-Number: +12404492163
Content: Thanks,
----SMS_END----
----SMS_START----
recv_time:2014-10-09 18:35:00
Span: 1
From-Number: +1347XXXXXXX
Timestamp: 14/10/09 18:34:37 96
Type: PDU
SMS-SMSC-Number: +12404492163
Content: Thanks
----SMS_END----
----SMS_START----
recv_time:2014-10-10 18:04:05
Span: 1
From-Number: +28809090
Timestamp: 14/10/09 23:03:42 96
Type: PDU
SMS-SMSC-Number: +12404492163
Content: AT&T Free Msg: If this was done in error, please call 611 or 800.901.9878.
----SMS_END----
答案 0 :(得分:0)
我猜您没有代码,这里有一些伪代码:
entries = {}
entry = empty
foreach lines in file do
if line is startTag then
entry = {} -- initilize empty entry
elseif line is endTag then
entries.add(entry)
else
entry.set(line.to(':'), line.till(':'))
end
end
这是算法(没有优化和容易出错)。
答案 1 :(得分:0)
试试这个工作
<?
$file = file("data.txt");
foreach($file as $key => $value)
{
$filter_value = trim($value);
if($filter_value!="" && $filter_value!="----SMS_START----" && $filter_value!="----SMS_END----"){
$new_array[] = $value;
$my_array = array_chunk($new_array, 7);
}
}
$count = count($my_array);
for($I=0;$I<$count;$I++){
foreach($my_array[$I] as $key => $value){
$ss = explode(":", $value);
$mm_array[$I][$ss[0]] = str_replace($ss[0].":", "", $value);
}
}
echo "<pre>";
print_r($mm_array);
echo "</pre>";
?>
结果
Array
(
[0] => Array
(
[recv_time] => 2014-10-09 18:32:39
[Span] => 1
[From-Number] => +1347XXXXXXX
[Timestamp] => 14/10/09 18:32:16 96
[Type] => PDU
[SMS-SMSC-Number] => +12404492163
[Content] => Thanks,
)
[1] => Array
(
[recv_time] => 2014-10-09 18:35:00
[Span] => 1
[From-Number] => +1347XXXXXXX
[Timestamp] => 14/10/09 18:34:37 96
[Type] => PDU
[SMS-SMSC-Number] => +12404492163
[Content] => Thanks
)
[2] => Array
(
[recv_time] => 2014-10-10 18:04:05
[Span] => 1
[From-Number] => +28809090
[Timestamp] => 14/10/09 23:03:42 96
[Type] => PDU
[SMS-SMSC-Number] => +12404492163
[Content] => AT&T Free Msg: If this was done in error, please call 611 or 800.901.9878.
)
)
您现在可以轻松插入数据库
享受!