Skip to content

Instantly share code, notes, and snippets.

@BenMorel
Created March 2, 2018 16:55
Show Gist options
  • Save BenMorel/4024bb1113a949840f97b0394d55dad3 to your computer and use it in GitHub Desktop.
Save BenMorel/4024bb1113a949840f97b0394d55dad3 to your computer and use it in GitHub Desktop.
PHP line breaks converter
<?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);
}
@BenMorel
Copy link
Author

BenMorel commented Mar 2, 2018

This is handy when line breaks are expected in a given format.
For example HTTP / MIME headers, mailto URLs, etc. only accept CRLF as per their respective RFCs.

Note that the order of the replacements above is important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment