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

时间:2019-05-22 15:22:24

标签: php regex crystal-reports reporting 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));
}

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

  1. 我不知道文件中可用的列数。可能因文件而异。

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

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

                                            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

1 个答案:

答案 0 :(得分:2)

怎么样?

//           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][0]),
            "name"=>trim($matches[2][0]),
            "addr"=>trim($matches[3][0]),
            //...
    );
}

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

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