0

I am atempting to extract the sort code and account number from a filename given that the first 6 figures represent the sort code and the last 8 figures represent the account number. An example of the filename would be:

./uploads/Santander/Statement_01020387654321.qif

What I have written does not seem to work, as I am new to regex maybe someone can explain how this should be done, or where I have gone wrong.

$sort = '';
$acno = '';

$ret = preg_match('/Statment_[0-9]{14}\.(csv|qif|qfx|ofx)$/', $file);

if ($ret)
{       
    if (ereg ('/_[0-9]{14}\./', $file, $regs))
    {
        $reg = $regs[count($regs)-1];
        $sort = substr($reg, 1, 6);
        $acno = substr($reg, 7, 8);
    }
}
1
  • 1
    Remember that the POSIX ereg() function is now deprecated, so you should be using the PCRE functions instead Commented Mar 7, 2011 at 11:31

4 Answers 4

1

I'm sure somebody versed with regular expressions can help you out, but it's possible without as well. It may be the more attractive option in the long run, because it's easier to maintain.

$path = "./uploads/Santander/Statement_01020387654321.qif"

$filename = pathinfo($path, PATHINFO_BASENAME); // "Statement_01020387654321.qif"

$temp_array = explode("_", $filename);

$sortcode = substr($temp_array[1], 0, 6); // 010203
$account = substr($temp_array[1], 6, 8);  // 7654321
Sign up to request clarification or add additional context in comments.

1 Comment

+1 : Was coding something on the similar grounds.You beat me to it :)
1

Do it in the first step of matching:

$ret = preg_match('/Statement_([0-9]{6})([0-9]{8})\.(csv|qif|qfx|ofx)$/', $file, $matches);

And in $matches you have info about sort number, account number and extension.

Comments

0

tested

echo $file ='./uploads/Santander/Statement_01020387654321.qif';

$ret = preg_match('/Statement_(\d{6})(\d{8})\.(csv|qif|qfx|ofx)$/', $file, $matches);

echo "<pre>";
print_r($matches);

returns

Array
(
    [0] => Statement_01020387654321.qif
    [1] => 010203
    [2] => 87654321
    [3] => qif
)

Comments

0

I think you just spelled "Statement" wrong in your Regex

In your Regex its: "Statment" without an "e"

1 Comment

Yes I had spelt it wrong but that was not all.. $ret = ereg ('/_[0-9]{14}\./', $file, $regs); did not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.