通过textareas合并关键字

时间:2014-05-21 09:10:07

标签: php

我尝试创建一个简短的脚本,它从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');
}

这是我用来创建表格的代码,还用结果填充“结果”textarea。

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]);
        }

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

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

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]);
        }

答案 1 :(得分:0)

试试这个:

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();