简体   繁体   English

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

[英]Extracting data from formatted plain text in PHP

I'm trying to convert some basic, formatted, plain text into a PHP array, so I can then further manipulate and display the data.我正在尝试将一些基本的、格式化的纯文本转换为 PHP 数组,以便我可以进一步操作和显示数据。

Below is an anonymized dataset of what I'm working with...以下是我正在使用的匿名数据集...

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

I have attempted to use substr();我曾尝试使用substr(); and preg_replace();preg_replace(); but with no luck.但没有运气。

Does anyone have any experience in data manipulation like this?有没有人有这样的数据操作经验? Would love some assistance!希望得到一些帮助!

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

you can use fgetcsv function and set the |您可以使用 fgetcsv 函数并设置 | as the separator for example:作为分隔符,例如:

?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);
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM