2 回答
TA贡献1934条经验 获得超2个赞
<?php
$text = "| |Customer |From |To |Bkg |Cabin |Class |Seat |Accept |
|1 |Palmer Lee Mr |JFK |LAX | |P |P |3K | |
|2 |Palmer Jamiee J Mrs |JFK |LAX |Y |P |P |3F |Boarded |
|3 |Coleman David Mr |JFK |LAX | |P |A |2F |Boarded |
|4 |Coleman Roberta Mrs |JFK |LAX |Y |P |A |2K | |
|5 |Williams Marina Lady |JFK |LAX | |P |F |1K | |
|6 |Graham Christine A Mrs |JFK |LAX | |P |P |4F | |
|7 |Graham Dean A Mr |JFK |LAX | |P |A |4A | |
|8 |Howell Fiona Ms |JFK |LAX | |P |A |5K | |
|9 |Cobb Hamish Mr |JFK |LAX | |P |A |1A | |
|10 |Brown Derek Mr |JFK |LAX | |P |P |2A |Accepted |
|11 |Brown Elizabeth Mrs |JFK |LAX | |P |P |3A |Accepted |
|12 |Reyes Ciaran Mrs |JFK |LAX | |P |F |5A |Standby |
|13 |Reyes Joseph Mr |JFK |LAX | |P |F |5F |Standby |";
//gets each line in a array
$pieces1 = explode("\n", $text);
// now create a loop if u want with array length and get each line and explode it again
// OR just callto any line by $pieces1[number]
$pieces2 = explode("|", $pieces1[3]);
// Remove first and last blank elements
$result = array_slice($pieces2, 1, -1);
?>
// print_r($result)
Array
(
[0] => 3
[1] => Coleman David Mr
[2] => JFK
[3] => LAX
[4] =>
[5] => P
[6] => A
[7] => 2F
[8] => Boarded
)
TA贡献1860条经验 获得超9个赞
您可以使用 fgetcsv 函数并设置 | 作为分隔符,例如:
?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
- 2 回答
- 0 关注
- 79 浏览
添加回答
举报