我可以在文件的开头和结尾检测换行符吗?此函数验证文件中的所有换行符?

问题描述 投票:0回答:1

我用这个函数来检测文件中的新行;但它返回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;
}
php file newline
1个回答
0
投票

解决问题我需要反转行数组,并使用break添加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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.