如何在PHP中使用带有循环的爆炸从html textarea获取输入

时间:2016-09-22 04:57:46

标签: php html

例如:文本区域中用户的输入如下:

123111_Testingzone
123222_Testinghouse
123333_Testingcity

输出应该在downloaded_file.txt中,它应该如下所示;

My testname is 123111
My testname is 123222
My testname is 123333

我的流程:

我尝试以html格式在一个textarea中的每一行中输入多个输入。试图在PHP中对其进行爆炸并将其循环以将其与预定义文本一起使用并输出为文件。

  • HTML代码:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Test/title>
      </head>
      <header>
        <h1>Test_Page</h1>
      </header>
    
      <body>
        <form action="abc.php" method="post">
            <h3>Option1</h3>
            Test Name: <textarea name="testname1"></textarea>
            <input type="submit" value="Download">
        </form>
    
      </body>
    </html>
    

    对于PHP代码,我只需要文本区域中每行的前6个字母,并在一个文件中输入和输出多个文本重复它。

  • PHP代码:

    <?php
    
    $testname = $_POST["testname1"];
    $array = explode('\n',$testname);
    $filename = "downloaded_file.txt";
    $testid = substr("{$array}",0,6);
    foreach ($testid as $content)
    {
    $contenter = "My test name is {$content}";
    }
    $f = fopen($filename, 'w');
    fwrite($f, $contenter);
    fclose($f);
    
    
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Length: ". filesize("$filename").";");
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Type: application/octet-stream; ");
    header("Content-Transfer-Encoding: binary");
    
    readfile($filename);
    
    
    ?>
    

我不知道我的代码在哪里出错以及如何纠正它?请帮忙。

6 个答案:

答案 0 :(得分:0)

首先拆分内容:

$array = explode(PHP_EOL,$testname);

然后你循环它:

foreach($array as $value){
  echo $value . '<br>';
}

现在你会看到:

123111_Testingzone
123222_Testinghouse
123333_Testingcity

在循环中,您应该用分隔符“_”分割。

foreach($array as $key => $value){
  $data = explode("_", $value);

  if(sizeof($data) == 1){ // make sure the delimiter is found and match the amount of splits, that should be 2 id:name -1.
    echo $data[0] . " is the number and " . $data[1] . " is the name<br>";
  } else {
    die("user did not post data on line '". $key+1 ."' properly.");
  }
}

现在您已将所有数据存储在文件中。而对于完整的代码:

<?php

  $testname = $_POST["testname1"];
  $filename = "downloaded_file.txt";
  $array = explode('\n',$testname);

  $f = fopen($filename, 'w');

  foreach($array as $key => $value){
    $data = explode("_", $value);

    if(sizeof($data) == 1){ // make sure the delimiter is found and match the amount of splits, that should be 2 id:name -1.
      fwrite($fh, "My testname is {$data[1]} and the id is {$data[0]}");
    } else {
      die("user did not post data on line '". $key+1 ."' properly.");
    }

  }
  fclose($fh);


  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-Length: ". filesize($filename).";");
  header("Content-Disposition: attachment; filename=$filename");
  header("Content-Type: application/octet-stream; ");
  header("Content-Transfer-Encoding: binary");

  readfile($filename);


?>

但是,您也可以只echo数据,而不是将其保存到文件然后输出。它是一样的,只有你没有麻烦的文件。如果这样做,请确保在输出之前已知标题。

答案 1 :(得分:0)

将此代码用于所需的结果

foreach ($array as $lines) {
    $testid = substr($lines,0,6);
    $contenter .= "My test name is {$testid}\n";
}

而不是以下代码

$testid = substr("{$array}",0,6);
foreach ($testid as $content)
{
$contenter = "My test name is {$content}";
}

答案 2 :(得分:0)

而不是fopenfclosefwrite,我使用file_put_contents,因为我个人之前遇到过fwrite的问题。

因此,这应该有用;

<?php

$testname = $_POST["testname1"];
$array = explode(PHP_EOL,$testname);
$filename = "downloaded_file.txt";
$testid = substr("{$array}",0,6);
foreach ($array as $val)
{
    $arr_contents = explode('_', $val);

    $content .= "My " . $arr_contents[1] . " is " . $arr_contents[0] . "\n";
    // \n is a newline char.
}
$filename = "downloaded_file.txt";
file_put_contents($filename, $content);
// write to file

?>

答案 3 :(得分:0)

代码的更正版本。评论内联。

<?php

// Initial input
$testname = $_POST["testname1"];

// Split on newline. You need double quotes here, as escape sequences for special characters like \n are not
// interpreted in single quoted strings
$array = explode("\n",$testname);

// Declare contenter string, outside foreach loop
$contenter = "";

// For each line
foreach ($array as $line)
{   

    // Take the first 6 characters
    $testid = substr($line,0,6);

    // Put testid in string, and append it to the $contenter string.
    $contenter .= "My test name is {$testid}\n";
}
// Write the whole thing to the file
$filename = "downloaded_file.txt";
$f = fopen($filename, 'w');
fwrite($f, $contenter);
fclose($f);


header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: text/plain; ");

readfile($filename);


?>

答案 4 :(得分:0)

$value = $request->get('testname1');
$skuList = explode(PHP_EOL, $value);

foreach ($skuList as $row)
{   
    $testid = substr("{$row}",0,6);
    $contenter = "My testname is {$testid}";
}

答案 5 :(得分:0)

尝试以下代码进行数组循环

foreach ($array as $content) {
        $contenter.= "My test name is ".substr($content, 0, 6)."\n";
    }