tags:

views:

109

answers:

7

Simple, right? Well, this isn't working :-\

$skuList = explode('\n\r', $_POST['skuList']);
A: 

For a new line, it's just

$list = explode("\n", $text);

For a new line and carriage return (as in Windows files), it's as you posted. Is your skuList a text area?

Andrew Sledge
Requires double quotes.
meagar
+9  A: 

Try "\n\r" (double quotes) or just "\n".

If you're not sure which type of EOL you have, run a str_replace before your explode, replacing "\n\r" with "\n".

Select0r
Single quotes in PHP mean "don't parse this string". That means your control characters aren't being parsed, they're being taken as literal (not a line break and a carriage return, but actual, literal '\n\r'). Using double quotes means "parse this string", and thus your control characters will be parsed. +1
Ryan Kinal
+7  A: 

Cover all cases. Don't rely that your input is coming from a Windows environment.

$skuList = preg_split("/\\n|\\r|\\r\\n/", $_POST['skuList']);

or

$skuList = preg_split('/\n|\r|\r\n/', $_POST['skuList']);
Alin Purcaru
A: 

Have you tried using double quotes?

piddl0r
+1  A: 

Place the \n in double quotes:

explode("\n", $_POST['skuList']);

In single quotes, if I'm not mistaken, this is treated as \ and n separately.

Russell Dias
A: 

First of all, I think it's usually \r\n, second of all, those are not the same on all systems. That will only work on windows. It's kind-of annoying trying to figure out how to replace new lines because different systems treat them differently (see here). You might have better luck with just \n.

Drackir
+3  A: 

Lots of things here:

  • You need to use double quotes, not single quotes, otherwise the escaped characters won't be escaped.
  • The normal sequence is \r\n, not \n\r.
  • Depending on the source, you may just be getting \n without the \r (or even in unusual cases, possibly just the \r)

Given the last point, you may find preg_split() using all the possible variants will give you a more reliable way of splitting the data than explode(). But alternatively you could use explode() with just \n, and then use trim() to remove any \r characters that are left hanging around.

Spudley