我有<textfield>
($_POST['list']
)。
如何获取数组键的每一行的值?
示例:
<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>
应转换为:
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]
)
感谢。
答案 0 :(得分:5)
使用explode函数,然后修剪结果数组(去除任何剩余的\n
,\r
或任何意外的空格/标签符号):
$lines = explode("\n", $_POST['list']);
$lines = array_map('trim', $lines);
答案 1 :(得分:4)
这是最安全的方法。它并不假设您可以丢弃回车符(\r
)字符。
$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);
答案 2 :(得分:2)
您可以使用explode()并在换行符\n
处展开。
$array = explode("\n", $_POST['list']);
答案 3 :(得分:2)
explode("\n", $_POST['list'])