从我的 preg_match 中排除文件扩展名

Excluding file extensions from my preg_match

我有一个脚本用于过滤一些 ROM 和 ISO 文件。

我(在很多帮助下)得到了一个工作脚本,其中文件按文件名过滤,但是我试图添加一个部分,我可以在其中包含额外的临时文件名,通过提供它们在本地 .txt 文件中。这工作正常,但是在我的 .txt 文件中,我必须将完整的文件名(包括 .txt 扩展名)放入 .txt - 例如我的“manualregiondupes.txt 文件如下所示:

Game One.zip
Game Two.zip
Game Three.zip

而我希望它像这样在我的 .txt 文件中键入它们:

Game One
Game Two
Game Three

我正在使用的当前正则表达式试图匹配它找到的完整文件名(包括 .zip 扩展名),而我希望它只匹配文件扩展名之前的部分。但是,我必须小心 - 因为我不想要这样的游戏:

如果 "Beach Volleyball (USA)" 在 .txt 中,则

"Summer Heat Beach Volleyball (USA)" 匹配。

另一边的字也一样 - 比如

如果 "Sensible Soccer (USA)" 在 .txt

中,则匹配

"Sensible Soccer (USA) (BETA)"

这是我的脚本;

// Make sure what manualregiondupes.txt is doing
if (file_exists('manualregiondupes.txt'))
{
    $manualRegionDupes = file('manualregiondupes.txt', FILE_IGNORE_NEW_LINES);
    $manualRegionPattern = "/^(?:" . implode("|", array_map(function ($i)
    {
        return preg_quote(trim($i) , "/");
    }
    , $manualRegionDupes)) . ')$/';
    echo "ManualRegionDupes.txt has been found, ";
    if (trim(file_get_contents('manualregiondupes.txt')) == false)
    {
        echo "but is empty! Continuing without manual region dupes filter.\n";
    }
    else
    {
        echo "and is NOT empty! Applying the manual region dupes filter.\n";
    }
} 
else
{
    echo "ManualRegionDupes.txt has NOT been found. Continuing without the manual region dupes filter.\n"; 
}


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


    // Filenames in manualregiondupes.txt
    if (file_exists('manualregiondupes.txt'))
    {
        if (trim(file_get_contents('manualregiondupes.txt')) == true)
        {
            if (preg_match($manualRegionPattern, $thisGame))
            {
                echo "{$thisGame} is on the manual region dupes remove list. Moved to Removed folder.\n";
                shell_exec("mv \"{$thisGame}\" Removed/");
                continue;
            }
        }
    }

    ... SCRIPT CONTINUES HERE BUT ISN'T RELEVANT!

最简单的方法是什么?我想我刚刚问了一个很长的问题,但它实际上很简单,但是哦,好吧 - 我对 PHP(或者老实说,任何脚本!)都不是很好,所以提前致歉并谢谢你! :D

您可以在正则表达式中使用路径信息,例如 -

$withoutExt = preg_replace('/\.' . preg_quote(pathinfo($path, PATHINFO_EXTENSION), '/') . '$/', '', $path);

它为您提供完美的文件名输出,没有扩展名

  1. file.txt -> 文件
  2. file.sometext.txt -> file.sometext