通过文本区域合并关键字


Merging keywords through textareas

我尝试创建一个简短的脚本,它从3个文本框中获取值,并将它们组合在一起。但是,我似乎无法将结果填充到文本框中。

这是我用来从文本框中获取数据并将其放入数组的PHP函数。

function get_text() {
    $text = trim($_POST['one']);
    $textAr = explode("'n", $text);
    $textAr = array_filter($textAr, 'trim');
    $text2 = trim($_POST['two']);
    $textAr2 = explode("'n", $text2);
    $textAr2 = array_filter($textAr2, 'trim');
    $text3 = trim($_POST['three']);
    $textAr3 = explode("'n", $text3);
    $textAr3 = array_filter($textAr3, 'trim');
}

下面是我用来创建表格的代码,并使用结果填充"result"文本区域。

if(isset($_POST['done'])) {
            get_text();
            for($i=0; $i < count($textAr); $i++)
                for($j; $j < count($textAr2); $j++)
                    for ($k; $k < count($textAr3); $k++)
                    echo($textAr[i] + $textAr2[j] + $textAr3[k]);
        }

问题是你的函数不返回任何东西

function get_text() {
    $text = trim($_POST['one']);
    $textAr = explode("'n", $text);
    $textAr = array_filter($textAr, 'trim');
    $text2 = trim($_POST['two']);
    $textAr2 = explode("'n", $text2);
    $textAr2 = array_filter($textAr2, 'trim');
    $text3 = trim($_POST['three']);
    $textAr3 = explode("'n", $text3);
    $textAr3 = array_filter($textAr3, 'trim');
    return array($textAr, $textAr2, $textAr3);
}

在上面的函数中,我添加了返回修改过的数组。在下面的语句中,我添加了使用get_gext函数的结果

if(isset($_POST['done'])) {
            list($textAr, $textAr2, $textAr3) = get_text();
          for($i=0; $i < count($textAr); $i++)
            for($j=0; $j < count($textAr2); $j++)
                for ($k=0; $k < count($textAr3); $k++)
                echo($textAr[$i] + $textAr2[$j] + $textAr3[$k]);
        }

在带有示例数据的整个工作代码下面:

<?php

$_POST['done'] = true;
$_POST['one'] = "1'n2'n3";
$_POST['two'] = "1'n2'n3";
$_POST['three'] = "1'n2'n3";
function get_text() {
    $text = trim($_POST['one']);
    $textAr = explode("'n", $text);
    $textAr = array_filter($textAr, 'trim');
    $text2 = trim($_POST['two']);
    $textAr2 = explode("'n", $text2);
    $textAr2 = array_filter($textAr2, 'trim');
    $text3 = trim($_POST['three']);
    $textAr3 = explode("'n", $text3);
    $textAr3 = array_filter($textAr3, 'trim');
    return array($textAr, $textAr2, $textAr3);
}

if(isset($_POST['done'])) {
            list($textAr, $textAr2, $textAr3) = get_text();

            for($i=0; $i < count($textAr); $i++)
                for($j=0; $j < count($textAr2); $j++)
                    for ($k=0; $k < count($textAr3); $k++)
                    echo($textAr[$i] + $textAr2[$j] + $textAr3[$k]);
        }

试试这个:

function get_text() {
    $text = trim($_POST['one']);
    $textAr = explode("'n", $text);
    $textAr = array_filter($textAr, 'trim');
    $text2 = trim($_POST['two']);
    $textAr2 = explode("'n", $text2);
    $textAr2 = array_filter($textAr2, 'trim');
    $text3 = trim($_POST['three']);
    $textAr3 = explode("'n", $text3);
    $textAr3 = array_filter($textAr3, 'trim');
    $allAr = array_merge($textAr, $textAr2, $textAr3);
    return implode(" ", $allAr);
}
echo get_text();