【问题标题】:Convert textarea to array and indented text to nested-arrays in PHP在 PHP 中将 textarea 转换为数组并将缩进文本转换为嵌套数组
【发布时间】: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 我没有问题检测到我想将它们嵌套在父数组下的行空间
  • 那么如果有空格或制表符则设置子数组

标签: php arrays textarea


【解决方案1】:

只是第一次尝试:

$str = <<<EOD
bar
    baz
        meh
        lol
            tuuut
moo
EOD;

function parse($lines, $depth = 0, $cur = 0)
{
    $retVal = array();

    for ($i = $cur; $i < count($lines); $i++)
    {
        $line = $lines[$i];
        $lDepth = strlen($line) - strlen(ltrim($line, " "));

        if ($lDepth == $depth)
        {
            $retVal[] = array("line" => ltrim($line, " "));
        } elseif ($lDepth == $depth + 4) {
            $children = parse($lines, $depth + 4, $i);
            $retVal[count($retVal) - 1]["children"] = $children;
            $i += count($children);
        }
    }

    return $retVal;
}

$lines = explode("\n", $str);
echo "<pre>";
print_r(parse($lines));

输出:

Array
(
    [0] => Array
        (
            [line] => bar
            [children] => Array
                (
                    [0] => Array
                        (
                            [line] => baz
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [line] => meh
                                        )

                                    [1] => Array
                                        (
                                            [line] => lol
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [line] => tuuut
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [line] => moo
        )

)

【讨论】:

  • 嘿。首先,我真的要感谢你的这个 sn-p。 I did some changes 因为我需要包含在 JSON 中的重要属性,该数组将被转换为。出于某种原因,当我添加另一个根元素(深度 = 0)时,代码“认为”它是前一个元素的根级别。 Gist 中包含的输入。非常感谢。
【解决方案2】:

用 4 个空格分隔:

$txt = preg_split('/    +/', $text);

【讨论】:

  • 我没有问题检测到我想将它们嵌套在父数组下的行空间
  • 你的父数组是什么,我好像听不懂你的问题
猜你喜欢
  • 2013-07-26
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
相关资源
最近更新 更多