在PHP中逐列读取rpt文件内容

我有.rpt文件,我需要读取它们的数据,然后将数据存储在MYSQL数据库中.我尝试了以下解决方案:

$lines = file("aa.rpt");
echo '<table>';
foreach ($lines as $line_num) {
        $str = trim(htmlspecialchars($line_num));
        echo '<tr>';
        echo '<td>' . getColumnText("/\s([a-zA-Z\s]+)/", $str) .'</td>';
        $secondCol = getColumnText("/\s([0-9]+)/", $str);
        echo '<td>' . $secondCol .'</td>';
        $thirdCol = end(explode(" ", $str));
        if (trim($secondCol) === $thirdCol) {
            echo '<td style="text-align:right">' . str_repeat(" ", 10) .'</td>';
        } else {
            echo '<td style="text-align:right">' . str_repeat(" ", 10) . $thirdCol .'</td>';
        }
        echo '</tr>';

}
echo '</table>';

function getColumnText($pattern, $str) {
    preg_match($pattern, $str, $matches);
    return trim(current($matches));
}

我从*的另一篇文章中获得了这个代码思想,但是我仍然有两个问题:

>我不知道文件中可用的列数.它可能因文件而异.
>每个文件都包含许多记录,其中某些记录头之后会再次重复.另外,标头在不同文件之间不一致,因此无论何时再次出现标头,我都无法估计要从顶部删除的行数和内容之间的行数.

我在记事本中编辑了该文件,将其作为示例粘贴到这里,但是我不确定是否所有隐藏的字符都会照原样复制.

                                            PSOMENAMEK                                                                                               PASREP 1/29
                                            ========================

                                            Address wise list of customers for loans and advances for Sol Id : 119600                 Date of Report           06-12-2018
                                            ===============================================================================
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SNO     Name of the Borrower                Communication Address                                                           City           State               Pin                 Customer Id    Account No          Mobile Number_1  Mobile Number_2  Mobile Number_3  Mobile Number_4  Pan Number  SchemeCode  Date of Loan          Limit      Purpose of Advance        Bal. O/S      BC Entered on
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


1      CHANDER SO RAMKALA AND MANSA RAM VP   VPO DALAMWALA,DIST JIND                                                        JIND CITY      HARYANA             126102              ABC111111      1111111111111111     1111111111                                                                      CCAKC       02-03-2009        1500000.00      RAISING CROPS            1505269.22   15-02-2017
2      ISHWAR  SO JITA                       VPO DALAMWALA,JIND                                                             JIND CITY      HARYANA             126102              ABC111111      1111111111111111     2222222222                                                                      CCAKC       02-03-2009         800000.00      RAISING CROPS             607925.78   06-02-2017
3      ROHTASH SO RAMPHAL                   #78/2 BLOCK NO 87 NEAR GOVT GIRLS SCHOOL,VPO SHAHPUR DISTT JIND HARYANA         JIND CITY      HARYANA             126125              ABC111111      1111111111111111     2222222222                                                                      CCAKC       30-10-2017         640000.00      RAISING CROPS             631757.63
4      RAM PHAL S/O SH GIANI RAM    KANDEL  112 BLOCK 96 KENDELA KANDELA,JIND                                               JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                        2222222222       3333333333                   CCAKC       28-10-2007         995000.00      RAISING CROPS             998043.05   01-09-2017
5      SAT NARAIN S/O SH JHANDU RAM   SRI   1156 13 BHUNA ROAD OLD BAJRANG ROAD,TOHANA RURAL DISTT FATEHABAD HARYANA        TOHANA DISTT-F HARYANA             125120              ABC111111      1111111111111111     1111111111                        3333333333       2222222222                   CCAKC       28-10-2007         600000.00      RAISING CROPS             354523.05   21-02-2017
6      RAJESH SO DARIYA SINGH               146 V RAJ PANA PO SHAHPUR TEH JIND,JIND HARYANA                                 JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                                                                      CCAKC       29-06-2018         550000.00      RAISING CROPS             245288.37
7      JAI BHAGWAN SO ATAR SINGH            1585/7 JAWAHAR NAGAR,PATIALA CHOWK                                              JIND CITY      HARYANA             126102              ABC111111      1111111111111111     3333333333                                                                      CCOTH       06-03-2017          40000.00      WORKING CAPITAL F          39636.00

                                            PSOMENAMEK                                                                                               PSTREP 1/29
                                            ========================

                                            Address wise list of customers for loans and advances for Sol Id : 119600                 Date of Report           06-12-2018
                                            ===============================================================================
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SNO     Name of the Borrower                Communication Address                                                           City           State               Pin                 Customer Id    Account No          Mobile Number_1  Mobile Number_2  Mobile Number_3  Mobile Number_4  Pan Number  SchemeCode  Date of Loan          Limit      Purpose of Advance        Bal. O/S      BC Entered on
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


172    RAJKUMAR SO JAGTU                    V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     1111111111                                                                      CCAKC       28-10-2007         300000.00      RAISING CROPS             294446.29   02-05-2016
173    RAM MEHAR S/O MOJI RAM               V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     2222222222                                                          AAAAAAAAAA  CCAKC       07-10-2010         400000.00      RAISING CROPS             401412.40   04-10-2015
174    RAMKARAN SO SHIVDHAN                 V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     1111111111                                                                      CCAKC       28-10-2007         550000.00      RAISING CROPS             417816.51   06-01-2017
175    RISHI LAL SO SHIVDHAN                V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111                                                                                     CCAKC       28-10-2007         995000.00      RAISING CROPS             997902.96   14-02-2017
176    SATYAWAN URF SATNARAIN S/O JAGE RAM  V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126110              ABC111111      1111111111111111     2222222222                                                                      CCAKC       17-11-2010         300000.00      RAISING CROPS UND         308424.76   09-12-2014
177    SHYAM S/O SATYA NARAYAN              V-JIWANPUR PO-DALAMWALA TEH-JIND,JIND HARYANA                                   JIND CITY      HARYANA             126125              ABC111111      1111111111111111     1111111111                                                                      CCAKC       16-05-2017         301000.00      RAISING CROPS             301836.88
178    SHAMSHER SINGH SO BIRU RAM           V-SIRIRAG PO-SHAHPUR TEH-JIND,JIND HARYANA                                      JIND CITY      HARYANA             126125              ABC111111      1111111111111111     3333333333                                                                      CCKGS       17-12-2007         600000.00      RAISING CROPS             603381.00   26-06-2018
179    JAI BHAGWAN S/O SH BARU RAM    SRI   V-SRI RAG KHERA PO- SHAHPUR TEH-JIND,JIND HARYANA                               JIND CITY      HARYANA             126125              ABC111111      1111111111111111     4444444444                                                          AAAAAAAAAA  CCAKC       28-10-2007         600000.00      RAISING CROPS             663130.17   26-06-2018

其他样品

BANK NAME                             Report Date:   06-12-2018               Page No.   :   1

                                                        Daily Outstanding Report



CIRCLE :  119600 - KANDELA                                                                     Amount in Account Currency
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
S No.  A/c No.          Name of the Account               Account  Dt of           Sanction Limit           Value of        Drawing Power             Balance              IRREGULARITY      Interest
                                                           Cur.     Sanction                                 Security                              Outstanding                               Rate        Clas
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Scheme Code : CCAKC
------------------------------------------
     1  1196008800001006 CHHAJJU RAM SO SH HIRA LAL        INR      28-01-2014         3,00,000.00        5,00,000.00          3,00,000.00         2,88,588.31 Dr                  0.00    7.00   STANDARD AS
     2  1196008800001158 RAMKARAN SO SHIVDHAN              INR      03-10-2016         5,50,000.00       11,00,000.00          5,50,000.00         4,17,816.51 Dr                  0.00   11.30   STANDARD AS
     3  1196008800001219 BALJIT SINGH SO SH RAM DHAN       INR      12-01-2015        18,00,000.00       30,00,000.00         18,00,000.00        17,99,175.06 Dr                  0.00   11.30   STANDARD AS
     4  1196008800001325 RAM NIWAS SO RAM KISHAN           INR      09-06-2014           30,000.00        1,50,000.00            30,000.00           30,402.62 Dr                402.62    7.00   STANDARD AS
     5  1196008800001352 SAJANPAL S/O SAMAT                INR      18-11-2015         1,50,000.00        3,00,000.00          1,50,000.00         1,47,677.50 Dr                  0.00   11.25   STANDARD AS
     6  1196008800001361 MUNSHI RAM SO SH JAI RAM    SR    INR      19-06-2015           80,000.00          80,000.00            80,000.00            1,585.63 Dr                  0.00   11.25   STANDARD AS
     7  1196008800001440 RICHH PAL SO RANPAT               INR      20-10-2018         2,50,000.00        4,00,000.00          2,50,000.00         2,49,819.36 Dr                  0.00    7.00   STANDARD AS
     8  1196008800001477 RAM DIYA SO HARI RAM              INR      19-05-2015           80,000.00        5,00,000.00            80,000.00           79,671.79 Dr                  0.00   14.00   STANDARD AS
     9  1196008800001486 SIYA RAM SO JOGI RAM              INR      01-05-2017         9,95,000.00       13,00,000.00          9,95,000.00         5,03,155.07 Dr                  0.00   11.30   STANDARD AS
    10  1196008800001547 GANGA BISAN S/O RAI SINGH         INR      27-03-2018         1,85,000.00        2,50,000.00          1,85,000.00         1,84,895.47 Dr                  0.00    7.00   STANDARD AS
    11  1196008800001556 SUNHERA SINGH SO MANGE RAM        INR      06-01-2014         4,00,000.00        8,00,000.00          4,00,000.00         1,79,359.39 Dr                  0.00    7.00   STANDARD AS

解决方法:

那这样的事呢

//           SNO     Name   Addr   City   State  Pin    CustId AccNo  Mobiles                     Pan    Scheme DateLoan               Limit  PrAdv  BalOs  BCEntered
$pattern = '/(\d+\s*)(.{37})(.{80})(.{15})(.{20})(.{20})(.{15})(.{21})(.{17})(.{17})(.{17})(.{17})(.{12})(.{12})(\d\d-\d\d-\d\d\d\d\s*)(.{15})(.{26})(.{12})((?:\d\d-\d\d-\d\d\d\d)?)/';

function readAccount($str) {
    if( !preg_match($pattern, $str, $matches) ) return null;
    return array(
            "sno"=>trim($matches[1]),
            "name"=>trim($matches[2]),
            "addr"=>trim($matches[3]),
            //...
    );
}

$lines = file("aa.rpt");
foreach ($lines as $line_num) {
    $str = trim(htmlspecialchars($line_num));
    $account = readAccount($str);
    if( $account)
            saveToDb($account);
} 

如果当前正在处理的行是无效的数据行,则readCustomer将不匹配并返回null.否则,方法将一一读取信息组,并返回帐户对象.在循环中,如果返回的值是有效帐户,则可以将其保存到db.

上一篇:开发陈同学-技多不压身CCIE之路


下一篇:如何写好一个“网络工程师”的求职简历