Created
May 21, 2024 07:59
-
-
Save ve3/bd320b18d04d075b252478c7ec03bba2 to your computer and use it in GitHub Desktop.
Normalize new line of large text file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Normalize new line from anything (CR, CRLF) to be LF only. | |
* This will be replace new line from a file, line by line. | |
*/ | |
// your file. | |
$textFile = '/path/to/my/file.txt'; | |
// maximum execution time should be increased depend on amount of lines in file. | |
set_time_limit(90); | |
// that's all, no need to modify below. | |
// prevent overwrite temp file. | |
if (is_file($textFile . '.tmp')) { | |
// if temp file is currently exists. | |
throw new \Exception('Temporary file is currently exists, please make sure and delete it before continue.'); | |
exit(); | |
} | |
$read = fopen($textFile, 'r'); | |
$write = fopen($textFile . '.tmp', 'w'); | |
if ($read && $write) { | |
while (!feof($read)) { | |
$line = fgets($read); | |
$line = preg_replace('~(*BSR_ANYCRLF)\R~', "\n", $line); | |
fwrite($write, $line); | |
} | |
}// endif; | |
// finish. -------------- | |
if ($read) { | |
fclose($read); | |
} | |
if ($write) { | |
fclose($write); | |
} | |
unset($read, $write); | |
if (is_file($textFile . '.tmp')) { | |
rename($textFile . '.tmp', $textFile); | |
// make sure that temporary file is not exists. | |
if (is_file($textFile . '.tmp')) { | |
unlink($textFile . '.tmp'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment