从 PHP 中的格式化纯文本中提取数据

我正在尝试将一些基本的、格式化的纯文本转换为 PHP 数组,这样我就可以进一步操作和显示数据。


下面是我正在使用的匿名数据集...


|    |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   |

我曾尝试使用substr();,preg_replace();但没有运气。


有没有人有这样的数据处理经验?希望得到一些帮助!


潇湘沐
浏览 82回答 2
2回答

撒科打诨

<?php&nbsp;$text = "|&nbsp; &nbsp; |Customer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|From |To&nbsp; &nbsp;|Bkg |Cabin |Class |Seat |Accept&nbsp; &nbsp; |&nbsp;|1&nbsp; &nbsp;|Palmer Lee Mr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|P&nbsp; &nbsp; &nbsp;|3K&nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;|2&nbsp; &nbsp;|Palmer Jamiee J Mrs&nbsp; &nbsp; &nbsp; &nbsp; |JFK&nbsp; |LAX&nbsp; |Y&nbsp; &nbsp;|P&nbsp; &nbsp; &nbsp;|P&nbsp; &nbsp; &nbsp;|3F&nbsp; &nbsp;|Boarded&nbsp; &nbsp;|&nbsp;|3&nbsp; &nbsp;|Coleman David Mr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|A&nbsp; &nbsp; &nbsp;|2F&nbsp; &nbsp;|Boarded&nbsp; &nbsp;|&nbsp;|4&nbsp; &nbsp;|Coleman Roberta Mrs&nbsp; &nbsp; &nbsp; &nbsp; |JFK&nbsp; |LAX&nbsp; |Y&nbsp; &nbsp;|P&nbsp; &nbsp; &nbsp;|A&nbsp; &nbsp; &nbsp;|2K&nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;|5&nbsp; &nbsp;|Williams Marina Lady&nbsp; &nbsp; &nbsp; &nbsp;|JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|F&nbsp; &nbsp; &nbsp;|1K&nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;|6&nbsp; &nbsp;|Graham Christine A Mrs&nbsp; &nbsp; &nbsp;|JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|P&nbsp; &nbsp; &nbsp;|4F&nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;|7&nbsp; &nbsp;|Graham Dean A Mr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|A&nbsp; &nbsp; &nbsp;|4A&nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;|8&nbsp; &nbsp;|Howell Fiona Ms&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|A&nbsp; &nbsp; &nbsp;|5K&nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;|9&nbsp; &nbsp;|Cobb Hamish Mr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|A&nbsp; &nbsp; &nbsp;|1A&nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |&nbsp;|10&nbsp; |Brown Derek Mr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|P&nbsp; &nbsp; &nbsp;|2A&nbsp; &nbsp;|Accepted&nbsp; |&nbsp;|11&nbsp; |Brown Elizabeth Mrs&nbsp; &nbsp; &nbsp; &nbsp; |JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|P&nbsp; &nbsp; &nbsp;|3A&nbsp; &nbsp;|Accepted&nbsp; |&nbsp;|12&nbsp; |Reyes Ciaran Mrs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|F&nbsp; &nbsp; &nbsp;|5A&nbsp; &nbsp;|Standby&nbsp; &nbsp;|&nbsp;|13&nbsp; |Reyes Joseph Mr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |JFK&nbsp; |LAX&nbsp; |&nbsp; &nbsp; |P&nbsp; &nbsp; &nbsp;|F&nbsp; &nbsp; &nbsp;|5F&nbsp; &nbsp;|Standby&nbsp; &nbsp;|";&nbsp; &nbsp; //gets each line in a array&nbsp; &nbsp; $pieces1 = explode("\n", $text);&nbsp; &nbsp; // now create a loop if u want with array length and get each line and explode it again&nbsp;&nbsp; &nbsp; // OR just callto any line by $pieces1[number]&nbsp; &nbsp; $pieces2 = explode("|", $pieces1[3]);&nbsp; &nbsp; // Remove first and last blank elements&nbsp; &nbsp; $result = array_slice($pieces2, 1, -1);?>// print_r($result)&nbsp; &nbsp; &nbsp; &nbsp; Array&nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; [0] => 3&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [1] => Coleman David Mr&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [2] => JFK&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [3] => LAX&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [4] =>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [5] => P&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [6] => A&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [7] => 2F&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; [8] => Boarded&nbsp; &nbsp;&nbsp; &nbsp; )

慕码人2483693

您可以使用 fgetcsv 函数并设置 | 作为分隔符,例如:?php$row = 1;if (($handle = fopen("test.csv", "r")) !== FALSE) {&nbsp; &nbsp; while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {&nbsp; &nbsp; &nbsp; &nbsp; $num = count($data);&nbsp; &nbsp; &nbsp; &nbsp; echo "<p> $num fields in line $row: <br /></p>\n";&nbsp; &nbsp; &nbsp; &nbsp; $row++;&nbsp; &nbsp; &nbsp; &nbsp; for ($c=0; $c < $num; $c++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $data[$c] . "<br />\n";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fclose($handle);}?>
打开App,查看更多内容
随时随地看视频慕课网APP