Created
March 2, 2018 16:55
-
-
Save BenMorel/4024bb1113a949840f97b0394d55dad3 to your computer and use it in GitHub Desktop.
PHP line breaks converter
This file contains hidden or 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 | |
/** | |
* Converts a string using CR, LF, CRLF, or possibly a mix of these, to the given EOL character(s). | |
* | |
* @param string $text The text to convert. | |
* @param string $eol The line break character(s). | |
* | |
* @return string | |
*/ | |
function convertEOL(string $text, string $eol) : string | |
{ | |
return str_replace(["\r\n", "\r", "\n"], ["\n", "\n", $eol], $text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is handy when line breaks are expected in a given format.
For example HTTP / MIME headers,
mailto
URLs, etc. only acceptCRLF
as per their respective RFCs.Note that the order of the replacements above is important.