views:

66

answers:

4
+2  Q: 

PHP read each line

I have a <textfield> ($_POST['list']).

How can I get value of each line to an array key?

Example:

<textfield name="list">Burnett River: named by James Burnett, explorer
Campaspe River: named for Campaspe, a mistress of Alexander the Great
Cooper Creek: named for Charles Cooper, Chief Justice of South Australia 1856-1861
Daintree River: named for Richard Daintree, geologist
</textfield>

Should be converted to:

Array(
[Burnett River: named by James Burnett, explorer]
[Campaspe River: named for Campaspe, a mistress of Alexander the Great]
[Cooper Creek: named for Charles Cooper, Chief Justice of South Australia 1856-1861]
[Daintree River: named for Richard Daintree, geologist]
)

Thanks.

+2  A: 

You can use explode() and explode at the newline character\n.

$array = explode("\n", $_POST['list']);
Brad F Jacobs
To whoever downvoted us, please elaborate with a comment. Thanks! Given Tim's answer, I will assume it was him. Simply state this, the above does work "most" of the time. There are a few circumstances, which you elaborated on, that it does not work. Does this merit a downvote, even if it does solve the answer, at least **most** of the time?
Brad F Jacobs
Most of the time time there _will_ be a carriage return.
Tim
Right, I understand that. However, even with the carriage return, the above still works. And the carriage return will **rarely** effect the application, other than being a left over character. The point is, this does solve the question, even if it may not be "the best" route.
Brad F Jacobs
I interpreted the question as asking how one would split an input string into an array by line breaks such that the first and last characters of each element were printable. If this data was getting saved anywhere (database or file) the extraneous characters could very well affect its integrity.
Tim
Yea, I can see that. Either or, it "should" work, but I would agree it is better to either trim, like aularon did, or do the replace. I would go with the `array_map` and `trim`, however, as my preference.
Brad F Jacobs
+1  A: 

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

Borealid
+5  A: 

Use explode function, then trim the result array (to get rid of any remaining \n, \r or any accidental spaces/tabs):

$lines = explode("\n", $_POST['list']);
$lines = array_map('trim', $lines);
aularon
+1 for the `array_map` / `trim`
Brad F Jacobs
+3  A: 

This is the safest way to do it. It doesn't assume you can just throw away carriage return (\r) characters.

$list_string = $_POST['list'];

// \n is used by Unix. Let's convert all the others to this format

// \r\n is used by Windows
$list_string = str_replace("\r\n", "\n", $list_string);

// \r is used by Apple II family, Mac OS up to version 9 and OS-9
$list_string = str_replace("\r", "\n", $list_string);

// Now all carriage returns are gone and every newline is \n format
// Explode the string on the \n character.
$list = explode("\n", $list_string);

Wikipedia: Newline

Tim