且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用php从txt文件中删除换行符

更新时间:2023-02-09 09:43:33

只需将file函数与FILE_IGNORE_NEW_LINES标志一起使用.

Just use file function with FILE_IGNORE_NEW_LINES flag.

file读取整个文件,并返回包含所有文件行的数组.

The file reads a whole file and returns an array contains all of the file lines.

默认情况下,每行末尾都包含换行符,但是我们可以通过FILE_IGNORE_NEW_LINES标志强制进行修剪.

Each line contains new line character at their end as default, but we can enforce trimming by FILE_IGNORE_NEW_LINES flag.

所以这很简单:

$lines = file('file.txt', FILE_IGNORE_NEW_LINES);

结果应为:

var_dump($lines);
array(5) {
    [0] => string(5) "Hello"
    [1] => string(5) "World"
    [2] => string(4) "John"
    [3] => string(4) "play"
    [4] => string(8) "football"
}