文件名上带有PHP的正则表达式

文件名上带有PHP的正则表达式,php,regex,Php,Regex,我试图从文件名中提取排序代码和帐号,因为前6位数字表示排序代码,后8位数字表示帐号。文件名的示例如下: ./uploads/Santander/Statement_01020387654321.qif 我写的东西似乎不起作用,因为我对regex还不熟悉,也许有人能解释一下应该怎么做,或者我哪里做错了 $sort = ''; $acno = ''; $ret = preg_match('/Statment_[0-9]{14}\.(csv|qif|qfx|ofx)$/', $file); if

我试图从文件名中提取排序代码和帐号,因为前6位数字表示排序代码,后8位数字表示帐号。文件名的示例如下:

./uploads/Santander/Statement_01020387654321.qif
我写的东西似乎不起作用,因为我对regex还不熟悉,也许有人能解释一下应该怎么做,或者我哪里做错了

$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);
    }
}

我相信精通正则表达式的人可以帮你,但是没有正则表达式也是可能的。从长远来看,它可能是更具吸引力的选择,因为它更容易维护

$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
我想你刚才在正则表达式中把“Statement”拼错了


在您的正则表达式中,没有“e”的“Statment”在匹配的第一步中执行:

$ret = preg_match('/Statement_([0-9]{6})([0-9]{8})\.(csv|qif|qfx|ofx)$/', $file, $matches);
$matches
中,您有关于排序号、帐号和扩展名的信息。

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
)
echo$file='./uploads/Santander/Statement_01020387654321.qif';
$ret=preg_match('/Statement(6})(\d{8})\.(csv | qif | qfx | ofx)$/',$file,$matches);
回声“;
打印(匹配项);
返回
排列
(
[0]=>语句_01020387654321.qif
[1] => 010203
[2] => 87654321
[3] =>qif
)

请记住,POSIX ereg()函数现在已被弃用,因此您应该改用PCRE函数+1:基于类似的理由编写了一些代码。您击败了我:)是的,我拼错了,但这还不是全部$ret=ereg('/[0-9]{14}\./',$file,$regs);不起作用。