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

For example: Input from user in text area will be as below:

123111_Testingzone
123222_Testinghouse
123333_Testingcity

Output should be in downloaded_file.txt and it should look like as below;

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

My process:

I try to take multiple input in each line in one textarea in html form. Trying to explode it in PHP and loop it to use it with predefined text and output as file.

  • HTML CODE:

    <!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>
    

    For PHP code, I want only first 6 letters of each line in text area and repeat it as many text enter and output in one file.

  • PHP CODE:

    <?php
    
    $testname = $_POST["testname1"];
    $array = explode('
    ',$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);
    
    
    ?>
    

I dont know where my code is wrong and how to correct it? Please help.

The corrected version of your code. Comments inline.

<?php

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

// Split on newline. You need double quotes here, as escape sequences for special characters like 
 are not
// interpreted in single quoted strings
$array = explode("
",$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}
";
}
// 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);


?>

First you split the content:

$array = explode(PHP_EOL,$testname);

Then you loop through it:

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

Now you will see:

123111_Testingzone
123222_Testinghouse
123333_Testingcity

Within the loop, you should split by a delimiter "_".

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.");
  }
}

Now you have all the data to store it in your file. And for the complete code:

<?php

  $testname = $_POST["testname1"];
  $filename = "downloaded_file.txt";
  $array = explode('
',$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);


?>

However, you can also just echo the data, instead of saving it to a file and then output it. It is the same, only you dont have to hassle with files. If you do this, make sure the headers are known before the output.

Use this code for the required result

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

Instead of the following code

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

Instead of fopen, fclose, fwrite, I use file_put_contents, as I personally have had issues with fwrite before.

Therefore, this should work;

<?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] . "
";
    // 
 is a newline char.
}
$filename = "downloaded_file.txt";
file_put_contents($filename, $content);
// write to file

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

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

Try following code for array loop

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