PHP读取每一行

标签 php arrays post textarea

我有一个 <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/

相关文章:

php - 显示或隐藏其他 Checkout 自定义字段的自定义下拉选择器

Php 变量自增数

php - MongoDB、数组存储和对象查询

python - 我应该使用哪种算法来优化我的代码?

c# - 在c#中将html转换为json

php - 我在 WAMP 安装的 httpd.conf 中输入了错误的信息...

php - HTML、MySQL 和 PHP -> 无文本框

c - 将数组分配给具有复杂情况的指针

java - 如何在java中读取客户端的restful响应以进行发布

php - 发送 POST 请求 wget