使用 PHP 解析 QIF 文件

发布于 2024-12-13 18:17:03 字数 234 浏览 1 评论 0原文

如何使用 PHP 解析此 QIF 文件?我希望每行都存储在每个“组”费用的变量中(分隔符是记录分隔符^)。谢谢!

!Type:Bank
D03/03/10
T-379.00
PCITY OF SPRINGFIELD
^
D03/04/10
T-20.28
PYOUR LOCAL SUPERMARKET
^
D03/03/10
T-421.35
PSPRINGFIELD WATER UTILITY
^

How can I parse this QIF file using PHP? I would like each line to be stored in a variable for each "set" of charges (delimiter is the record separator ^). Thanks!

!Type:Bank
D03/03/10
T-379.00
PCITY OF SPRINGFIELD
^
D03/04/10
T-20.28
PYOUR LOCAL SUPERMARKET
^
D03/03/10
T-421.35
PSPRINGFIELD WATER UTILITY
^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-12-20 18:17:03

我的 Codeigniter 项目的库中有一个函数可以执行此操作。看看这是否有帮助。

/**
 * Will process a given QIF file. Will loop through the file and will send all transactions to the transactions API.
 * @param string $file 
 * @param int $account_id 
 */
function qif($file, $account_id)
{       

    $obj =& get_instance();

    $lines = file($file);

    $records = array();
    $record = array();
    $end = 0;

    foreach($lines as $line)
    {
        /*
        For each line in the QIF file we will loop through it
        */

        $line = trim($line);

        if($line === "^")
        {
            /* 
            We have found a ^ which indicates the end of a transaction
            we now set $end as true to add this record array to the master records array
            */

            $end=1;

        }
        elseif(preg_match("#^!Type:(.*)$#", $line, $match))
        {
            /*
            This will be matched for the first line.
            You can get the type by using - $type = trim($match[1]);
            We dont want to do anything with the first line so we will just ignore it
            */
        }
        else
        {
            switch(substr($line, 0, 1))
            {
                case 'D':
                    /*
                    Date. Leading zeroes on month and day can be skipped. Year can be either 4 digits or 2 digits or '6 (=2006).
                    */
                    $record['date']   = trim(substr($line, 1));
                    break;
                case 'T':
                    /*
                    Amount of the item. For payments, a leading minus sign is required. 
                    */
                    $record['amount'] = trim(substr($line, 1));
                    break;
                case 'P':
                    /*
                    Payee. Or a description for deposits, transfers, etc.

                    Note: Yorkshite Bank has some funny space in between words in the description
                    so we will get rid of these
                    */
                    $line = htmlentities($line);
                    $line = str_replace("  ", "", $line);
                    //$line = str_replace(array("£","£"), 'GBP', $line);

                    $record['payee']  = trim(substr($line, 1));
                    break;
                case 'N':
                    /*
                    Investment Action (Buy, Sell, etc.).
                    */
                    $record['investment']  = trim(substr($line, 1));
                    break;
            }

        }


        if($end == 1)                                     
        {
            // We have reached the end of a transaction so add the record to the master records array
            $records[] = $record;

            // Rest the $record array
            $record = array();

            // Set $end to false so that we can now build the new record for the next transaction
            $end = 0;
        }

    }

    foreach($records as $my_record)
    {

        $date = explode('/', $my_record['date']);
        $new_date = date("Y-m-d", mktime('0', '0', '0', $date[1], $date[0], $date[2]));




        $new_transaction = new stdClass;
        $new_transaction->transaction_account_id    = $account_id;
        $new_transaction->transaction_ref       = '';
        $new_transaction->transaction_date      = $new_date;
        $new_transaction->transaction_amount        = $my_record['amount'];
        $new_transaction->transaction_uid           = md5($my_record['date'] . $my_record['amount'] . $account_id . $obj->session->userdata('userdetails')->user_id);
        $new_transaction->transaction_name      = urlencode(xss_clean($my_record['payee']));
        $new_transaction->transaction_split_id      = '0';


        $create_result = $obj->fsm_restapi->createTransaction($new_transaction);


    }

}

I have a function that I have in a library in my Codeigniter project which does this. See if this helps at all.

/**
 * Will process a given QIF file. Will loop through the file and will send all transactions to the transactions API.
 * @param string $file 
 * @param int $account_id 
 */
function qif($file, $account_id)
{       

    $obj =& get_instance();

    $lines = file($file);

    $records = array();
    $record = array();
    $end = 0;

    foreach($lines as $line)
    {
        /*
        For each line in the QIF file we will loop through it
        */

        $line = trim($line);

        if($line === "^")
        {
            /* 
            We have found a ^ which indicates the end of a transaction
            we now set $end as true to add this record array to the master records array
            */

            $end=1;

        }
        elseif(preg_match("#^!Type:(.*)$#", $line, $match))
        {
            /*
            This will be matched for the first line.
            You can get the type by using - $type = trim($match[1]);
            We dont want to do anything with the first line so we will just ignore it
            */
        }
        else
        {
            switch(substr($line, 0, 1))
            {
                case 'D':
                    /*
                    Date. Leading zeroes on month and day can be skipped. Year can be either 4 digits or 2 digits or '6 (=2006).
                    */
                    $record['date']   = trim(substr($line, 1));
                    break;
                case 'T':
                    /*
                    Amount of the item. For payments, a leading minus sign is required. 
                    */
                    $record['amount'] = trim(substr($line, 1));
                    break;
                case 'P':
                    /*
                    Payee. Or a description for deposits, transfers, etc.

                    Note: Yorkshite Bank has some funny space in between words in the description
                    so we will get rid of these
                    */
                    $line = htmlentities($line);
                    $line = str_replace("  ", "", $line);
                    //$line = str_replace(array("£","£"), 'GBP', $line);

                    $record['payee']  = trim(substr($line, 1));
                    break;
                case 'N':
                    /*
                    Investment Action (Buy, Sell, etc.).
                    */
                    $record['investment']  = trim(substr($line, 1));
                    break;
            }

        }


        if($end == 1)                                     
        {
            // We have reached the end of a transaction so add the record to the master records array
            $records[] = $record;

            // Rest the $record array
            $record = array();

            // Set $end to false so that we can now build the new record for the next transaction
            $end = 0;
        }

    }

    foreach($records as $my_record)
    {

        $date = explode('/', $my_record['date']);
        $new_date = date("Y-m-d", mktime('0', '0', '0', $date[1], $date[0], $date[2]));




        $new_transaction = new stdClass;
        $new_transaction->transaction_account_id    = $account_id;
        $new_transaction->transaction_ref       = '';
        $new_transaction->transaction_date      = $new_date;
        $new_transaction->transaction_amount        = $my_record['amount'];
        $new_transaction->transaction_uid           = md5($my_record['date'] . $my_record['amount'] . $account_id . $obj->session->userdata('userdetails')->user_id);
        $new_transaction->transaction_name      = urlencode(xss_clean($my_record['payee']));
        $new_transaction->transaction_split_id      = '0';


        $create_result = $obj->fsm_restapi->createTransaction($new_transaction);


    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文