使用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);


    }

}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...