我有这个脚本:
$dl = array('st' => false, 'smg' => '');
$fileR = file($PhatToFile);
$fileR = array_reverse($fileR);
$c = count($fileR) + 1;
foreach ($fileR as $line) {
if (!strlen(rtrim($line))) {
$dl['smg'] .= 'Incorrect space found file: <b> ' . $file . '</b> Line: <b>' . $c . '</b><br>';
$dl['st'] = true;
$c = $c - 1;
} else {
break;
}
}
if($dl['st'] == true){
echo $dl['smg'];
}
我用它来确定文件是否以空格结尾或换行符结尾,但不适用于此:
<?php
echo "hello world";
?> (Line break)
(no find this... line 5 have line break and file end in 6)
检查我是否列举了每一行。
请尝试一下,我已替换了您的if语句。
foreach ($fileR as $line) {
if (!trim(preg_replace('/\s+/', '', $line))) {
$dl['smg'] .= 'Incorrect space found file: <b> ' . $file . '</b> Line: <b>' . $c . '</b><br>';
$dl['st'] = true;
$c = $c - 1;
} else {
break;
}
}
我解决这个问题,有能力检测:
文件开头的空格或换行符。
文件末尾的空格或换行符。
$dl = array('st' => false, 'smg' => '');
$smg1 = $smg3 = '';
$smg2 = [];
$fileR = file($file);
$c = 1;
foreach ($fileR as $line) {
if (!strlen(trim($line))) {
$smg1 .= 'Incorrect space/Linebreak found file: <b> ' . $file . '</b> Line: <b>' . $c . '</b><br>';
$dl['st'] = true;
$c++;
} else {
break;
}
}
$fileR = array_reverse($fileR);
$rc = count($fileR);
foreach ($fileR as $line) {
if (!strlen(trim($line))) {
$smg2[] = 'Incorrect space/Linebreak found file: <b> ' . $file . '</b> Line: <b>' . $rc . '</b><br>';
$dl['st'] = true;
$rc--;
} else {
break;
}
}
$fileR = array_reverse($fileR);
if ((substr($fileR[$rc - 1], -1) == "\n") AND strlen(trim($fileR[$rc - 1])) > 0) {
$smg3 .= 'Incorrect Linebreak found file: <b> ' . $file . '</b> Line: <b>' . $rc . '</b><br>';
$dl['st'] = true;
}
$smg2 = array_reverse($smg2);
echo $dl['smg'] = $smg1 . $smg3 . implode('', $smg2);