如何将这个preg_match添加到我的PHP脚本中?

时间:2018-06-25 15:15:02

标签: php regex preg-match

我有一个脚本,用于查找具有较高修订版本的文件(基于文件命名约定)。

如果存在最终电子表格(Rev B).xls ,则会删除

I.E 最终电子表格(Rev A).xls 。它的版本号也很好。我希望添加功能以实现基本相同的功能-但要包含没有“ Rev”的文件,因为某些文件名根本没有“ Rev”。

例如,我想为以下功能添加功能:

如果存在 Ultimate Powerpoint(Rev A).ppt Ultimate Powerpoint(Rev B).ppt ,则要删除

Ultimate Powerpoint.ppt 。我想我几乎已经有代码可以使用上面提到的第一条规则来执行此操作了,但是我没有亲自编写它,并且对PHP非常陌生,因此想知道是否有人可以向我指出正确的方向。非常感谢!我当前的代码:

$fileList = trim(shell_exec("ls -a -1"));
$fileArray = explode("\n", $fileList);

shell_exec('mkdir -p Removed');



// Do this magic for every file
foreach ($fileArray as $thisFile)
{
    if (!$thisFile) continue;
    // Probably already been removed
    if (!file_exists($thisFile)) continue;

    $niceName = trim(preg_replace('%[\(|\[].*%', '', $thisFile));
    preg_quote(pathinfo($thisGame, PATHINFO_EXTENSION), '/') . '$/', '', $thisFile);


// Check for reversions e.g. (Rev 2) or (Rev A)
    if (preg_match('%\(Rev (\d|[A-Za-z])\)%', $thisFile, $revNum))
    {
        if (is_numeric($revNum[1]))
        {
            $revNum = intval($revNum[1]);
        }
        else
        {
            $revNum = ord($revNum[1]);
        }
    }

    $otherVersions = trim(shell_exec("ls -1 \"{$niceName} (\"*\"(Rev \"* 2>/dev/null"));
    if ($otherVersions)
    {
        $otherVersionArray = explode("\n", $otherVersions);
        foreach ($otherVersionArray as $otherFile)
        {
            preg_match('%\(Rev (\d|[A-Za-z])\)%', $otherFile, $thisRev);
            if (is_numeric($thisRev[1]))
            {
                $thisRev = intval($thisRev[1]);
            }
            else
            {
                $thisRev = ord($thisRev[1]);
            }

            if (isset($revNum) && $revNum < $thisRev)
            {
                // Other version is newer, bin ours
                echo "{$thisFile} has an inferior version number/letter [{$revNum} VS. {$thisRev}] - Moved to Removed folder.\n";
                shell_exec("mv ".escapeshellarg($thisFile)." Removed/");
                continue 2;
            }
        }
    }
}

0 个答案:

没有答案