Last day I had tried a lot of methods to retrieve lines from a text file. In some methods, I got blank lines without any reason and sometimes it returns all lines as one. In Windows the end of line is '\r\n' , in Mac its \r and Unix its \n. PHP explode function will fail here to split line by line, because if we use \n, sometime you won’t get the expected results, and same in the case of \r\n.
Finally I tried to trim all \r from the file content then use preg_split function to split by \n. This works fine for me.
PHP Code
$file_content = file_get_contents('readme.txt');
// Removing all carriage returns
$file_content = preg_replace("/\r/", "", $file_content);
// Split lines with new line
$lines = preg_split("/\n/", $file_content);
If you have any other better solution for this problem, please share with me.

Share Your Thoughts