Reputation: 171
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);
}
}
Upvotes: 0
Views: 215
Reputation: 54016
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
)
Upvotes: 0
Reputation: 121
I think you just spelled "Statement" wrong in your Regex
In your Regex its: "Statment" without an "e"
Upvotes: 0
Reputation: 152206
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.
Upvotes: 1
Reputation: 449385
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
Upvotes: 1