php - 我可以只检测文件开头和结尾的换行符吗?此函数验证文件中的所有换行符?
问题描述
我使用这个函数来检测文件中的新行;但它返回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;
}
解决方案
为了解决这个问题,我需要反转行的数组,并添加带有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;
}
推荐阅读
- excel - Excel VBA:在用户窗体中连接滚动条和文本框
- python - 使用 BeatifulSoup 解析 HTML 中的文本块 (IndustryAbout)
- google-apps-script - Populating html sidebar with js variable data
- string - 比较相邻链表节点中包含的字符串中的单个字符
- android - 从其他方法访问 savedInstance
- jquery - Chrome throwing an Aw Snap in debugger when trying to get form control value with JQuery
- python - 在 virtualenv 中设置 pygame
- r - R:四舍五入有什么问题?
- java - 方法同名但参数不同
- ansible - 无法使用 ansible 创建 gitlab 项目