我有一个 <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]
)
谢谢。
最佳答案
使用explode函数,然后修剪结果数组(以去除任何剩余的 \n
、\r
或任何意外的空格/制表符):
$lines = explode("\n", $_POST['list']);
$lines = array_map('trim', $lines);
关于PHP读取每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3619546/