【发布时间】:2016-12-27 08:08:58
【问题描述】:
我想从 PHP 中的 Form Textarea POST 中获取文本输入,并遍历每一行文本并使用文本创建一个 PHP 数组。
问题是我想获取在它们之前缩进 4 个空格的文本行,并使这些行成为上述数组项下的子嵌套数组。
我目前不知道如何做到这一点,所以如果有任何想法,我将不胜感激。
// basic demo to show each line of textarea post
$text = $_POST['textarea'];
foreach(explode("\n", $text) as $line) {
echo $line;
echo '<br>';
}
更新
到目前为止的一个想法可能是这样的开始......
$text = $_POST['textarea'];
$in_nested_array = false;
$array = array();
foreach(explode("\n", $text) as $line) {
if($line is 4 spaces){
$in_nested_array = true;
$array[''][$line];
}else{
//if in nested array and new line is not nested, add to root array
if($in_nested_array){
}else{
$in_nested_array = false;
$array[] = $line;
}
}
}
【问题讨论】:
-
我不确定但你试过
\t或爆炸后开头的空格数吗? -
@SougataBose 我没有问题检测到我想将它们嵌套在父数组下的行空间
-
那么如果有空格或制表符则设置子数组