php - 检查文件内容是否在没有字符的换行符处结束
问题描述
我有这个脚本:
$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;
}
}
推荐阅读
- fonts - 微软计算机视觉可以提取字体信息吗
- c# - 安全地从另一个线程访问控制
- angular - 从 Angular 应用程序内部与外部使用 Cypress.io 有什么好处?
- javascript - 将属性附加到猫鼬文档的最佳方法是什么
- laravel - 使用自定义工匠 make 命令生成文件 Laravel
- python - 当我尝试计算百分比并放入新数据框时的 NAN 值
- linux - /bin/sh: /usr/sbin/sshd-keygen: 没有那个文件或目录
- jquery - 使用asp.net mvc中的ajax删除表中的行,而无需在jquery数据表中回发
- javascript - checkCollision 函数在 Array.forEach 中不起作用(检查元素碰撞)
- linux - 错误: sudo: 需要密码\r\n - 尽管用户对 sudoers 具有权限