txt文件,如下所示:
first item1:second item1:third item1:fourth item1:fifth item1
first item2:second item2:third item2:fourth item2:fifth item2
first item3:second item3:third item3:fourth item3:fifth item3
我希望能够输出每个项目。
据我所知,我需要:
file()
explode()
按:
我尝试了各种各样的东西,但我无法让它发挥作用。谁知道怎么做?
答案 0 :(得分:0)
所以你需要这样的东西吗?
$lines = file('try.txt');
$output = array();
foreach ($lines as $line) {
$output[] = array_map(function($var) {
return rtrim($var, "\r\n");
}, explode(":", $line));
}
var_dump($output);
输出
array
0 =>
array
0 => string 'first item1' (length=11)
1 => string 'second item1' (length=12)
2 => string 'third item1' (length=11)
3 => string 'fourth item1' (length=12)
4 => string 'fifth item1' (length=11)
1 =>
array
0 => string 'first item2' (length=11)
1 => string 'second item2' (length=12)
2 => string 'third item2' (length=11)
3 => string 'fourth item2' (length=12)
4 => string 'fifth item2' (length=11)
2 =>
array
0 => string 'first item3' (length=11)
1 => string 'second item3' (length=12)
2 => string 'third item3' (length=11)
3 => string 'fourth item3' (length=12)
4 => string 'fifth item3' (length=11)