2

In my web application, I create a formula which uses a textarea to take a list of IDs (in hex). The user have to separate them by pressing return after entering every ID.

<label class="col-md-3 control-label" for="id_list">IDs: </label>
<div class="col-md-9>
    <textarea class="form-control" id="id_list" name="id_list" required></textarea>
</div>

Then, I send this list to the server for processing (in PHP):

$id_list = trim($_POST['id_list'];
$id_array = explode(PHP_EOL, $id_list);

// file_put_contents('/tmp/bla.txt', $id_array);

for($i = 0; $i < count($id_array); $i++){
  // Do stuff
}

// Now I want to save the ids in a string which will separate the ids with ","
$id_string = str_replace(PHP_EOL, ',', $id_list)

This works fine on a Windows server. However, on a Linux server it seems to have problem with PHP_EOL; when I put the result from explode(PHP_EOL, $id_list) in a txt file and examine it in Notepad++, the CR is still there.

I tried different ways'\n','\r\n',..., but it seems using PHP_EOL is the only way where explode(PHP_EOL, $id_list) works in Windows.

What can I do to make it working on both Windows AND Linux?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yangsunny
  • 656
  • 5
  • 13
  • 32
  • Did you try escaping the newline characters? e.g. `\\r\\n`? – Michiel Pater Feb 25 '16 at 11:00
  • Did you try using ``nl2br()`` function? – uglypointer Feb 25 '16 at 11:02
  • if i read http://stackoverflow.com/a/14217315/5378743 correctly linebreaks in textareas are always (no matter which operating system) send as `"\r\n"`. – Roland Starke Feb 25 '16 at 11:06
  • 1
    I just found my mistake, and its a stupid one... \r\n works fine on both Windows and Linux. BUT I need to put them in `""` and not `''`. After I changed that, everything works fine – yangsunny Feb 25 '16 at 11:08
  • strange though, in http://stackoverflow.com/questions/5769589/explode-error-r-n-and-n-in-windows-and-linux-server , someone had problem with "\r\n" on both platform and PHP_EOL was suggested as solution, but here is just the other way around. Does something changed since 2011? – yangsunny Feb 25 '16 at 11:25

3 Answers3

1

I actually (almost) had the right answer. \r\n works on both systems, but I just made the mistake putting them in '' (single quotes) when "" (double quotes) should be used here:

explode("\r\n", $id_list)
str_replace("\r\n", ',', $id_list)

It is both working on Windows and Linux now.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yangsunny
  • 656
  • 5
  • 13
  • 32
0
$id_list = trim($_POST['id_list']);
$id_array = explode("\n", $id_list);
$id_array = array_filter($id_array, 'trim');
foreach ($id_array as $line) {
    // do stuff here
} 

This should work, you explode on "\n" and trim any left over "\r" (if any).

Guy
  • 1,254
  • 17
  • 16
0

I suggest you to use preg_split, therefore a regex:

$id_array = preg_split('#[\s,]+#Sui', $id_list);

So you have an array of ID, whatever the input format is. You can check the manual entry for this function in preg_split (php.net).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JesusTheHun
  • 1,217
  • 1
  • 10
  • 19