Creating an array from a list of items in a $_POST variable

Routinely, I’ll find my self creating forms that will take a list of items in a textarea that was pasted from another program like Excel ( column of data ). Furthermore, I will usually take this list of data and explode it into an array to be processed individually.

Using the explode function in php is simple, however, when separating items in the $_POST array by a newline – the newline may be different based on the type of php server.

\n is used for Unix systems (including Linux, and OSX).

\r\n is mainly used on Windows.

\r is used on really old Macs.

To avoid these differences, you can use the php constant PHP_EOL .

If we had a textarea named “list” – then it would look like: