PHP的“继续”似乎无法正常工作
作者:互联网
我有一个遍历目录中每个文件名并删除不需要的文件的脚本.在其他方面,它匹配CSV文件中的文件名,然后删除相邻单元格中的文件.
<?php
$gameList = trim(shell_exec("ls -a -1 -I . -I .. -I Removed"));
$gameArray = explode("\n", $gameList);
shell_exec('mkdir -p Removed');
echo "\033[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 "\033[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 "\033[01;32m\n<<<<<<<<<<Process Complete>>>>>>>>>>\n\n";
?>
它正在工作,但是我不明白为什么我会在“ ECHO ZEBRA”或“ ECHO LION”之后立即看到最后的回显“ Scanned and Kept”-因为我在它们之后直接有“ continue”调用,应该重新启动循环并移至下一个文件.我敢肯定,我只需要在某个地方重新摇动一下东西,但是我已经摆弄了几个小时,我完全陷入了困境.如果有任何帮助,我将非常感谢!非常感谢!
解决方法:
continue
仅用于读取单个行的内循环.如果要跳到外循环的末尾,则需要使用…
continue 2;
这使您可以说继续2级循环.
标签:fgetcsv,continue,while-loop,php 来源: https://codeday.me/bug/20191025/1925799.html