首页 > 解决方案 > 检查文件内容是否在没有字符的换行符处结束

问题描述

我有这个脚本:

$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)

检查我已经枚举了每一行。

标签: phpline-breaksspace

解决方案


请试试这个,我已经替换了你的 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;
    }
}

推荐阅读