PDA

View Full Version : function to store lines of textarea in array?


chump2877
08-16-2005, 11:57 AM
Is there a PHP function that will do something similar to what file() does (when it reads in each line of a text file and stores it as an array element), but apply this behavior to a POST variable that represents the contents of a HTML textarea? So this function would read the contents of my POST variable, line by line, and store each new line in an array element....Ideas?

Thanks.


Edit: Here's a better question: Does explode() recognize line breaks in a HTML textarea as '\n'? Or are line breaks lost in translation so to speak when the textarea contents are assigned to a PHP variable? So the resulting string becomes one long, contiguous string without line breaks?

marek_mar
08-16-2005, 12:42 PM
Yes explode does recognise \n as newline.
You must use it in double-quotes.

<?php
$input = 'Hello
World
!';
$array = explode("\n", $input);
var_dump($array);
?>

devinemke
08-16-2005, 03:48 PM
be advised: different OS's use different characters for newlines:

Win = /r/n
UNIX = /n
Mac = /r

the newlines in the incoming POST textarea will depend entirely on what OS the client is using to send the data.

chump2877
08-16-2005, 04:46 PM
Are we talking server-side OS's or client-side OS's here?

devinemke
08-16-2005, 04:55 PM
Are we talking server-side OS's or client-side OS's here?
client. the form is filled out on the client and sent to the server so the newlines will be in whatever format the client sent.