This conversion can be done by simple apps from the Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making fewer function calls. The function [str_replace][2]
str_replace()
can accept arrays, so there is no need for embedded calls.
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
But I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.
This conversion can be done by simple apps from the Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making fewer function calls. The function [str_replace][2]
can accept arrays, so there is no need for embedded calls.
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
But I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.
This conversion can be done by simple apps from the Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making fewer function calls. The function str_replace()
can accept arrays, so there is no need for embedded calls.
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
But I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.
This conversion can be done by simple apps from the Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making fewer function calls. The function [str_replace][2]
can accept arrays, so there is no need for embedded calls.
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
But I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.
This conversion can be done by simple apps from the Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making fewer function calls. The function [str_replace][2]
can accept arrays, so there is no need for embedded calls.
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
But I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.
This conversion can be done by simple apps from the Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making fewer function calls. The function [str_replace][2]
can accept arrays, so there is no need for embedded calls.
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
But I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.
SuchThis conversion can be done by simple apps from the Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making lessfewer function calls. FunctionThe function [str_replace][2]
can accept arrays, so there is no need for embedded calls:.
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
butBut I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.
Such conversion can be done by simple apps from Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making less function calls. Function [str_replace][2]
can accept arrays, so there is no need for embedded calls:
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
but I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.
This conversion can be done by simple apps from the Dos2Unix package.
However, if you insist on writing your own converter, I'd suggest making fewer function calls. The function [str_replace][2]
can accept arrays, so there is no need for embedded calls.
$contents = str_replace(
array("\r", "\n", "[%LINEBREAK%][%LINEBREAK%]", "[%LINEBREAK%]"),
array("[%LINEBREAK%]", "[%LINEBREAK%]", "[%LINEBREAK%]", "\r\n"),
fread($this->fp, filesize($this->filename))
);
However, you ought to be careful. For example, if your file has two Unix-standard newlines, i.e., \n\n
, then your code will replace this with \r\n
, which is only one Windows-standard newline.
This regex (see about its syntax here) would probably do the trick:
preg_replace('#(?<!\r)\n|\r(?!\n)#', "\r\n", fread($this->fp, filesize($this->filename)));
But I still think it's better to use already existing and proven software, like the one from the beginning of this answer, or simply turn on the auto_detect_line_endings setting, as suggested in the comments by 200_success.
By the way, if your files are huge, your fread
will still (try to) read them whole at once, which is not pleasant for the computer memory and performance.