使用 PHP 将 TextArea 转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16518434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:17:27  来源:igfitidea点击:

TextArea to Array with PHP

phphtml

提问by Hugo Delsing

I'm trying to figure out how to convert html textarea into php array,

我想弄清楚如何将 html textarea 转换为 php 数组,

I've used a form with POST to deliver the query to the php script, and the php file is getting them with the following line:

我使用带有 POST 的表单将查询传递给 php 脚本,并且 php 文件通过以下行获取它们:

$ids = array($_POST['ids']);

Needless to say that it puts everything into one line

不用说它把所有东西都放在一行

Array ( [0] => line1 line2 line3 line4 ) 

I need the final results to replace this:

我需要最终结果来替换这个:

$numbers = array(
"line1",
"line2",
"line3",
"line4"
);

What would be the best approach to divide and re-parse it ?

划分和重新解析它的最佳方法是什么?

回答by Hugo Delsing

Using an explode on \nis a proper way to get new lines. keep in mind though that on some platforms the end of line is actually send by \r\n, so just exploding on \ncould leave you with extra data on the end of each line.

使用爆炸\n是获得新线条的正确方法。请记住,尽管在某些平台上,行尾实际上是由 发送的\r\n,所以只是爆炸\n可能会在每行的末尾留下额外的数据。

My suggestion would be to remove the \rbefore exploding, so you dont have to loop through the entire array to trim the result. As a last improvement, you dont know that there actually is a $_POST['ids'], so always check it first.

我的建议是删除\rbefore exploding,这样你就不必遍历整个数组来修剪结果。作为最后的改进,你不知道实际上有一个$_POST['ids'],所以总是先检查它。

<?
$input = isset($_POST['ids'])?$_POST['ids']:"";

//I dont check for empty() incase your app allows a 0 as ID.
if (strlen($input)==0) {
  echo 'no input';
  exit;
}

$ids = explode("\n", str_replace("\r", "", $input));
?>

回答by Johan Vranckx

I would've done the explode by Hugo like this:

我会像这样由 Hugo 完成爆炸:

$ids = explode(PHP_EOL, $input);

manual Predefined Constants

手动预定义常量

Just my two cents...

只是我的两分钱...

回答by user2511140

Use this

用这个

$new_array = array_values(array_filter(explode(PHP_EOL, $input)));

explode-> convert textarea to php array (that lines split by new line)
array_filter-> remove empty lines from array
array_values-> reset keys of array

explode-> 将 textarea 转换为 php 数组(由新行分割的行)
array_filter-> 从数组中删除空行
array_values-> 重置数组的键

回答by enenen

Try with explode function:

尝试使用爆炸功能

$ids = $_POST['ids']; // not array($_POST['ids'])
$ids = explode(" ", $ids); 

The first parameter is the delimiterwhich could be space, new line character \r\n, comma, colon etc. according to your string from the textarea (it's not clear from the question whether values are separated by spaces or by new lines).

第一个参数是根据您来自 textarea 的字符串delimiter可能是空格、换行符\r\n、逗号、冒号等(从问题中不清楚值是由空格分隔还是由新行分隔)。

回答by Ian Wood

If the textarea simply has line breaks per entry then I'd do something like:

如果 textarea 每个条目只有换行符,那么我会执行以下操作:

$ids = nl2br($_POST['ids');
$ids = explode('<br />',$ids); //or just '<br>' depending on what nl2br uses.