duanjiancong4860 2016-09-22 04:57
浏览 46
已采纳

如何在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.

  • 写回答

6条回答 默认 最新

  • duanqinjiao5244 2016-09-22 05:14
    关注

    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);
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab