[英]How to remove skip empty line when I read a file
reading the official manual , if I want to skip empty line in a txt I just need to call the function file() with FILE_SKIP_EMPTY_LINES; 阅读官方手册 ,如果我想在txt中跳过空行我只需要用FILE_SKIP_EMPTY_LINES调用函数文件(); in fact I wrote :
事实上我写道:
$fileArray=file($uploadDirectory.$tracklistFile_name, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
but It also read empty line. 但它也读了空行。 So, in a text like this :
所以,在这样的文字中:
// START OF THE DOCUMENT
aaa
bbb
ccc
ddd
// END OF THE DOCUMENT
in fact sizeof($fileArray)
is 6 : it adds the empty lines between bbb
and ccc
, and the last one. 实际上
sizeof($fileArray)
是6:它在bbb
和ccc
之间添加空行,最后一行。 Why? 为什么?
FILE_SKIP_EMPTY_LINES
really only skips empty lines. FILE_SKIP_EMPTY_LINES
实际上只跳过空行。 If you have spaces or tabs or other whitespace in it, file()
will not consider it an empty line. 如果您有空格或制表符或其他空格,
file()
将不会将其视为空行。
It doesn't do a trim
before checking, it looks if $line=="\\n"
and only then considers it an empty line. 它在检查之前不会进行
trim
,它会查看$line=="\\n"
,然后才将其视为空行。
As alternative you could use: 作为替代,您可以使用:
$file = array_filter(array_map("trim", file("text.txt")), "strlen");
有很多方法可以清理阵列,但我喜欢这个:
$fileArray = array_values(array_filter($fileArray, "trim"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.