2011-11-03 64 views
3

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

你有什麼試過的? SO不是「請爲我寫這個代碼」的網站。請閱讀http://stackoverflow.com/questions/how-to-ask –

回答

5

我有一個函數,我已經在我的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); 


    } 

}