remove new line characters from txt file using php

前端 未结 4 1190
南方客
南方客 2020-12-06 11:02

I have txt file its content like this

Hello  
World   
John  
play  
football  

I want to delete the new line character when reading this

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 11:46

    Just use file function with FILE_IGNORE_NEW_LINES flag.

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

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

    So it will be simply:

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

    The result should be:

    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"
    }
    

提交回复
热议问题