如何将textarea值作为单独的变量?

时间:2018-05-08 08:01:25

标签: php variables textarea display

我的Html代码是

<html>
<body>
<form method="post" action="">
<textarea name='test'></textarea>
<input type='submit'/>
</form>
</body>
</html>

我在textarea的输入为

Hi
How
are
you

按下回车键,在textarea的下一行输入输入。

现在正在显示

<?php 
echo "<pre>";
echo $array = $_POST['test'];
echo "</pre>";
?>

给出结果为

Hi
How
are
you

结果将存储在同一个变量下。是否可以在单独的变量中获取每个行值?

2 个答案:

答案 0 :(得分:0)

这是答案;

<?php 
echo "<pre>";
print_r(explode("\n", $_POST['test']));
echo "</pre>";
?>

答案 1 :(得分:0)

试试这个。希望它有所帮助。

$var = $_POST['test'];

$array = array_values(array_filter(explode(PHP_EOL, $var)));

$variables = [];
$i = 1;
foreach ($array as $row){

    $variables["variable_$i"] = $row;
$i++;
}

$var1 = $variables["variable_1"];
$var2 = $variables["variable_2"];
$var3 = $variables["variable_3"];

echo "1st variable ".$var1." 2nd variable ".$var2."3th variable ".$var3;