输入示例:
$document='(400, 530); 6.9; 5.7; 5.0; ...
(500, 530); 7.9; 5.1; 5.0; ...
(600, 530); 6.7; 6.7; 7.2; ...';
方法#1(不带分号的值存储在数组中):
foreach(explode("\r\n",$document) as $row){ // split the content by return then newline
$result[]=explode("; ",$row); // split each row by semi-colon then space
}
var_export($result);
/* Output:
[
['(400, 530)','6.9','5.7','5.0','...'],
['(500, 530)','7.9','5.1','5.0','...'],
['(600, 530)','6.7','6.7','7.2','...']
]
) */
方法#2(带分号的值存储在数组中):
foreach(explode("\r\n",$document) as $row){ // split the content by return then newline
$result[]=preg_split('/(?<!,) /',$row); // split each row by space not preceeded by comma
}
var_export($result);
/* Output:
[
['(400, 530);','6.9;','5.7;','5.0;','...'],
['(500, 530);','7.9;','5.1;','5.0;','...'],
['(600, 530);','6.7;','6.7;','7.2;','...']
]
) */
Here is 两种方法的演示 http://sandbox.onlinephpfunctions.com/code/88b5eaead7f50bec6b50934a3e49a1ce7537a3f9.
请记住,我只关注循环内的字符串分割。克里斯关于文件处理的建议是可取的。
根据您的环境,您可能需要通过删除来调整第一次爆炸\r
或类似的。