我使用此函数来检测文件中的新行;但是它返回true,所有新行检测包括文件中的所有行;但我只需要在开头和结尾进行过滤。
// $file is the path plus name of the file location localy.
private function CheckFileIntegrity($file) {
$dl = false;
$fileR = file($file);
foreach ($fileR as $line) {
if (!strlen(trim($line))) {
$dl = true;
}
}
return $dl;
}
为了解决这个问题,我需要反转行数组,并添加带有中断的 else 语句:
private function CheckFileIntegrity($file) {
$dl = [];
$dl['st'] = false;
$dl['smg'] = '';
$fileR = file($file);
$c = 0;
foreach ($fileR as $line) {
$c++;
if (!strlen(trim($line))) {
$dl['smg'] .= 'Incorrect space found the file: <b>' . $file . '</b> Line: <b>' . $c . '</b>';
$dl['st'] = true;
} else {
break;
}
}
if ($dl == false) {
$fileR = array_reverse($fileR);
foreach ($fileR as $line) {
if (!strlen(trim($line))) {
$dl['smg'] .= 'Incorrect space found the file: <b>' . $file . '</b> Line: <b>' . $c . '</b>';
$dl['st'] = true;
} else {
break;
}
}
}
return $dl;
}