微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用PHP解析QIF文件

如何使用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
^

解决方法:

我有一个函数,我在我的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);


    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐