Kingsquare has developed a MT940 file parser which reads the given file or string, determines the (dutch) bank and applies a parser. The output from the parser is a manageable and easily usable format of PHP objects which can be passed on for further processing.

The parser tries to determine which originating bank it is from via the first few lines of the file and then loads up the engine per bank.
This way the developer has the ability to extend the actual bank-engine quite easily by overruling / defining its own set of rules to determine the bank.

Since most banks adhere to a standard a default can also be used for parsing the files and statements within.

The downloadable package provides several pieces:

  • banking objects
    the actual storage (struct classes) for data retention and easy access.
  • parsers
    per bank a different parser can be utilized and customized as see fit
  • parser unittests
    ofcourse several (simple) unittests are provided to ensure correct parsing

Installation and examples

Installation is handled by composer

composer require kingsquare/php-mt940

After this a simple example is provided by the package:


<?php
// composer autoloader
require dirname(__DIR__) . 'vendor/autoload.php';
// instantiate the actual parser
// and parse them from a given file, this could be any file or a posted string
$parser = new \Kingsquare\Parser\Banking\Mt940();
$tmpFile = __DIR__ . '/test.mta';
$parsedStatements = $parser->parse(file_get_contents($tmpFile));

The parsed statements is a simple array containing Statement_Banking objects which each contain possible Transaction_Banking objects Further processing could be something in the lines of:


<?php
// continued from above
foreach ($parsedStatements as $statement) {
	$transactions = $statement->getTransactions();
	// walk the transactions and try to find matches
	foreach ($transactions as $transaction) {
		echo $transaction->getAccountName().' made a transaction for the amount of '.$transaction->getPrice();
	}
}

Ofcourse this too could be extended to include matching the descriptions (or account numbers, amounts etc) to possible customer information. 

Source

The sources are available on Github and packagist.