Разбор файла QIF с использованием PHP - PullRequest
3 голосов
/ 03 ноября 2011

Как я могу разобрать этот файл QIF, используя PHP?Я хотел бы, чтобы каждая строка сохранялась в переменной для каждого «набора» расходов (разделитель - разделитель записей ^).Спасибо!

!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
^

1 Ответ

6 голосов
/ 27 января 2012

У меня есть функция, которая есть в библиотеке в моем проекте 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);


    }

}
...