从.txt文件获取精确的preg_replace匹配项

时间:2018-06-22 08:54:57

标签: php regex preg-replace

我有一个脚本,其中.txt格式的行分隔列表(例如:

Summer Heat Beach Volleyball.zip
Beach Volleyball.zip
Pacman.zip
Space Invaders.zip

用于搜索目录中的每个文件名以查找匹配项,并将这些匹配项放置在“已删除”文件夹中。

<?php
$gameList = trim(shell_exec("ls -1"));
$gameArray = explode("\n", $gameList);

shell_exec('mkdir -p Removed');
shell_exec('mkdir -p Process');


// 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 "ManualRegeionDupes.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;

    $niceName = trim(preg_replace('%[\(|\[].*%', '', $thisGame));


    // 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;
            }
        }
    }

唯一的问题是,如果名字像

Beach Volleyball.zip

在manualregiondupes.txt列表中,文件名为

Summer Heat Beach Volleyball.zip

也被过滤器阻塞。如何执行精确匹配?

0 个答案:

没有答案