Reputation: 43647
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.
Upvotes: 2
Views: 375
Reputation: 2403
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);
Upvotes: 4