preg_match - 从 .txt 文件中的行开始(带空格和括号!)
preg_match -ing from lines in a .txt file (with spaces and brackets!)
所以我靠自己走到了这一步,但看起来我已经找到了我的 PHP 知识的极限(根本不是很多!)。此脚本用于过滤文件名(游戏 roms/iso 等)。它也有其他过滤方式,但我只是突出显示了我要添加的部分。我想要一个外部 .txt 文件,我可以像这样放入文件名(由一个换行符分隔):
Pacman 2 (USA)
Space Invaders (USA)
Asteroids (USA)
Something Else (Europe)
然后 运行 脚本将搜索目录并将任何匹配的文件名放入 "removed" 文件夹中。它可以很好地循环使用它使用的所有其他过滤技术。我只是想添加我自己的(不成功!)
$gameList = trim(shell_exec("ls -1"));
$gameArray = explode("\n", $gameList);
$file = file_get_contents('manualremove.txt');
$manualRemovePattern = '/(' . str_replace(PHP_EOL, "|", $file) . ')/';
shell_exec('mkdir -p Removed');
foreach($gameArray as $thisGame) {
if(!$thisGame) continue;
// Probably already been removed
if(!file_exists($thisGame)) continue;
if(preg_match ($manualRemovePattern , $thisGame)) {
echo "{$thisGame} is on the manual remove list. Moving to Removed folder.\n";
shell_exec("mv \"{$thisGame}\" Removed/");
continue;
因此,当我在 .txt 文件中输入不带空格或括号的游戏名称时,这是有效的。但是空格或括号(或两者)破坏了它的功能。有人可以帮我吗?
非常感谢!
将您提供的代码中的第四行替换为
$manualRemovePattern = "/(?:" . implode("|", array_map(function($i) {
return preg_quote(trim($i), "/");
}, explode(PHP_EOL, $file))) . ')/';
主要思想是:
- 将您获得的文件内容拆分为
explode(PHP_EOL, $file)
行
- 然后你需要遍历数组并修改数组中的每一项(可以用
array_map
完成)
- 修改数组项涉及在任何特殊的正则表达式元字符和您选择的正则表达式分隔符(在本例中为
/
)之前添加转义 \
,这是通过 preg_quote(trim($i), "/")
- 请注意,我从数组项中删除了带有
trim
的所有 leading/trailing 空格 - 以防万一。
要将它们作为整个单词进行匹配,请使用 单词边界:
$manualRemovePattern = '/\b(?:' . implode('|', array_map(function($i) {
return preg_quote(trim($i), '/');
}, explode(PHP_EOL, $file))) . ')\b/';
要将它们作为整个字符串进行匹配,请使用 ^
/$
anchors:
$manualRemovePattern = '/^(?:' . implode('|', array_map(function($i) {
return preg_quote(trim($i), '/');
}, explode(PHP_EOL, $file))) . ')$/';
所以我靠自己走到了这一步,但看起来我已经找到了我的 PHP 知识的极限(根本不是很多!)。此脚本用于过滤文件名(游戏 roms/iso 等)。它也有其他过滤方式,但我只是突出显示了我要添加的部分。我想要一个外部 .txt 文件,我可以像这样放入文件名(由一个换行符分隔):
Pacman 2 (USA)
Space Invaders (USA)
Asteroids (USA)
Something Else (Europe)
然后 运行 脚本将搜索目录并将任何匹配的文件名放入 "removed" 文件夹中。它可以很好地循环使用它使用的所有其他过滤技术。我只是想添加我自己的(不成功!)
$gameList = trim(shell_exec("ls -1"));
$gameArray = explode("\n", $gameList);
$file = file_get_contents('manualremove.txt');
$manualRemovePattern = '/(' . str_replace(PHP_EOL, "|", $file) . ')/';
shell_exec('mkdir -p Removed');
foreach($gameArray as $thisGame) {
if(!$thisGame) continue;
// Probably already been removed
if(!file_exists($thisGame)) continue;
if(preg_match ($manualRemovePattern , $thisGame)) {
echo "{$thisGame} is on the manual remove list. Moving to Removed folder.\n";
shell_exec("mv \"{$thisGame}\" Removed/");
continue;
因此,当我在 .txt 文件中输入不带空格或括号的游戏名称时,这是有效的。但是空格或括号(或两者)破坏了它的功能。有人可以帮我吗?
非常感谢!
将您提供的代码中的第四行替换为
$manualRemovePattern = "/(?:" . implode("|", array_map(function($i) {
return preg_quote(trim($i), "/");
}, explode(PHP_EOL, $file))) . ')/';
主要思想是:
- 将您获得的文件内容拆分为
explode(PHP_EOL, $file)
行
- 然后你需要遍历数组并修改数组中的每一项(可以用
array_map
完成) - 修改数组项涉及在任何特殊的正则表达式元字符和您选择的正则表达式分隔符(在本例中为
/
)之前添加转义\
,这是通过preg_quote(trim($i), "/")
- 请注意,我从数组项中删除了带有
trim
的所有 leading/trailing 空格 - 以防万一。
要将它们作为整个单词进行匹配,请使用 单词边界:
$manualRemovePattern = '/\b(?:' . implode('|', array_map(function($i) {
return preg_quote(trim($i), '/');
}, explode(PHP_EOL, $file))) . ')\b/';
要将它们作为整个字符串进行匹配,请使用 ^
/$
anchors:
$manualRemovePattern = '/^(?:' . implode('|', array_map(function($i) {
return preg_quote(trim($i), '/');
}, explode(PHP_EOL, $file))) . ')$/';