character separator for newline textarea

Go To StackoverFlow.com

0

So evidently:

\n = CR (Carriage Return) // Used as a new line character in Unix
\r = LF (Line Feed) // Used as a new line character in Mac OS
\r\n = CR + LF // Used as a new line character in Windows
(char)13 = \n = CR // Same as \n

but then I also heard that for HTML textarea, when it's submitted and parsed by a php script, all new lines are converted to \r\n regardless of the platform

is this true and can I rely on this or am I completely mistaken?

ie. if I wanna do explode() based on a new line, can I use '\r\n' as the delimiter regardless of whether or not the user is using mac, pc, etc

2012-04-04 21:10
by pillarOfLight
Not true, but see here: How to replace different newline styles in PHP the smartest way?hakre 2012-04-04 21:14


2

All newlines should be converted in \r\n by the spec.

So you could indeed do a simple explode("\r\n", $theContent) no matter the platform used.

P.S.

\r is only used on old(er) Macs. Nowadays Macs also use the *nix style line breaks (\n).

2012-04-04 21:23
by PeeHaa


1

You could try preg_split, which will use a regular expression to split up the string. Within this regular expression you can match on all 3 new line variants.

$ArrayOfResults = preg_split( '/\r\n|\r|\n/', $YourStringToExplode );
2013-07-25 09:22
by Paul G.


0

It depends on what you want to achieve. If you are doing this eventually to display / format it as HTML, you can as well use the nl2br() function or possibly use str_replace like this:

$val = str_replace( array("\n","\r","\r\n"), '<br />', $val );

In case you want to just get an array of all lines, I would suggest you use all 3 characters ("\n","\r","\r\n") for explode

2012-04-04 21:20
by Akshay Raje
Ads