php "continue" 貌似不行
php "continue" seemingly not working
我有一个脚本,它遍历目录中的每个文件名并删除不需要的文件。还有很多,它匹配 CSV 文件中的文件名,然后删除相邻单元格中的文件。
<?php
$gameList = trim(shell_exec("ls -a -1 -I . -I .. -I Removed"));
$gameArray = explode("\n", $gameList);
shell_exec('mkdir -p Removed');
echo "3[01;32m\n\n<<<<<<<<<<Starting Scan>>>>>>>>>>\n\n";
// Do the magic for every file
foreach ($gameArray as $thisGame)
{
// Ensure continue if already removed
if (!$thisGame) continue;
if (!file_exists($thisGame)) continue;
// Manipulate names to look and play nice
$niceName = trim(preg_replace('%[\(|\[].*%', '', $thisGame));
$thisGameNoExtension = preg_replace('/\.' . preg_quote(pathinfo($thisGame, PATHINFO_EXTENSION) , '/') . '$/', '', $thisGame);
// Let the user know what game is being evaluated
echo "3[00;35m{$thisGameNoExtension} ... ";
$f = fopen("ManualRegionDupes.csv", "r");
while ($row = fgetcsv($f))
{
if ($row[1] == $thisGameNoExtension)
{
$primaryFile = $row[0];
$ext = pathinfo($thisGame, PATHINFO_EXTENSION);
$fCheck = trim(shell_exec("ls -1 " . escapeshellarg($primaryFile) . "." . escapeshellarg($ext) . " 2>/dev/null"));
if ($fCheck)
{
echo "ECHO LION";
shell_exec("mv " . escapeshellarg($thisGame) . " Removed/");
continue;
}
else
{
echo "ECHO ZEBRA";
continue;
}
break;
}
}
fclose($f);
echo "Scanned and kept";
}
echo "3[01;32m\n<<<<<<<<<<Process Complete>>>>>>>>>>\n\n";
?>
它正在工作,但是我不明白为什么我在 "ECHO ZEBRA" 或 "ECHO LION" 之后直接看到最终回显 "Scanned and Kept" - 因为我有 "continue" 调用紧随其后,这应该会重新启动循环并转到下一个文件。我确定我只需要在某个地方重新调整一些东西,但我已经摆弄了几个小时而且我完全被难住了。如果有任何帮助,我将非常感激!非常感谢!
你接完电话就只有休息了,还有什么运行?编辑,如果你想继续外循环,把它移到那里。继续只是在错误的循环中。
continue
仅适用于读取各行的内部循环。如果你想跳到外循环的末尾,你将需要使用...
continue 2;
这允许你说继续 2 级循环。
我有一个脚本,它遍历目录中的每个文件名并删除不需要的文件。还有很多,它匹配 CSV 文件中的文件名,然后删除相邻单元格中的文件。
<?php
$gameList = trim(shell_exec("ls -a -1 -I . -I .. -I Removed"));
$gameArray = explode("\n", $gameList);
shell_exec('mkdir -p Removed');
echo "3[01;32m\n\n<<<<<<<<<<Starting Scan>>>>>>>>>>\n\n";
// Do the magic for every file
foreach ($gameArray as $thisGame)
{
// Ensure continue if already removed
if (!$thisGame) continue;
if (!file_exists($thisGame)) continue;
// Manipulate names to look and play nice
$niceName = trim(preg_replace('%[\(|\[].*%', '', $thisGame));
$thisGameNoExtension = preg_replace('/\.' . preg_quote(pathinfo($thisGame, PATHINFO_EXTENSION) , '/') . '$/', '', $thisGame);
// Let the user know what game is being evaluated
echo "3[00;35m{$thisGameNoExtension} ... ";
$f = fopen("ManualRegionDupes.csv", "r");
while ($row = fgetcsv($f))
{
if ($row[1] == $thisGameNoExtension)
{
$primaryFile = $row[0];
$ext = pathinfo($thisGame, PATHINFO_EXTENSION);
$fCheck = trim(shell_exec("ls -1 " . escapeshellarg($primaryFile) . "." . escapeshellarg($ext) . " 2>/dev/null"));
if ($fCheck)
{
echo "ECHO LION";
shell_exec("mv " . escapeshellarg($thisGame) . " Removed/");
continue;
}
else
{
echo "ECHO ZEBRA";
continue;
}
break;
}
}
fclose($f);
echo "Scanned and kept";
}
echo "3[01;32m\n<<<<<<<<<<Process Complete>>>>>>>>>>\n\n";
?>
它正在工作,但是我不明白为什么我在 "ECHO ZEBRA" 或 "ECHO LION" 之后直接看到最终回显 "Scanned and Kept" - 因为我有 "continue" 调用紧随其后,这应该会重新启动循环并转到下一个文件。我确定我只需要在某个地方重新调整一些东西,但我已经摆弄了几个小时而且我完全被难住了。如果有任何帮助,我将非常感激!非常感谢!
你接完电话就只有休息了,还有什么运行?编辑,如果你想继续外循环,把它移到那里。继续只是在错误的循环中。
continue
仅适用于读取各行的内部循环。如果你想跳到外循环的末尾,你将需要使用...
continue 2;
这允许你说继续 2 级循环。