I wanted to know if there's a way to manipulate line breaks within PHP. Like, to explicitly tell what kind of line break to select (LF, CRLF...) for using in an explode()
function for instance.
it would be something like that:
$rows = explode('<LF>', $list);
//<LF> here would be the line break
anyone can help? thanks (:
LF and CR are just abbreviations for the characters with the code point 0x0A (LINE FEED) and 0x0D (CARRIAGE RETURN) in ASCII. You can either write them literally or use appropriate escape sequences:
"\x0A" "\n" // LF
"\x0D" "\r" // CR
Remember using the double quotes as single quotes do only know the escape sequences \\
and \'
.
CRLF would then just be the concatenation of both characters. So:
$rows = explode("\r\n", $list);
If you want to split at both CR and LF you can do a split using a regular expression:
$rows = preg_split("/[\r\n]/", $list);
And to skip empty lines (i.e. sequences of more than just one line break characters):
$rows = preg_split("/[\r\n]+/", $list);