Andris
Andris

Reputation: 1442

html textearea with multiple lines. Want to convert to php array, each line is one element of array

In textarea i type words, each in new line. Type a word, press Enter. Like this

first
second
third

Want to get php array like this

[0] => first
[1] => second
[2] => third

Tried

$arr_list_from_textarea = explode( PHP_EOL, $_POST['list_from_textarea'] );

But got

Array
(
[0] => first
second
third
)

What need to change? Instead of PHP_EOL something else?

"\n" works. But '\n' and "\r\n" do not work.

Upvotes: 1

Views: 33

Answers (1)

alex
alex

Reputation: 490423

Don't use that constant. Use "\n" (use double quotes so the string is not treated literally, but the actual character).

PHP_EOL is used for writing files, etc so you can handily grab the platform specific end-of-line character. When handling user input, browsers are kind enough to always give you \n as the line separator.

Upvotes: 3

Related Questions