【问题标题】:How to use explode with loop in PHP taking input from html textarea如何在PHP中使用explode with loop从html textarea获取输入
【发布时间】:2016-09-22 04:57:46
【问题描述】:

例如:用户在文本区输入如下:

123111_Testingzone
123222_Testinghouse
123333_Testingcity

输出应在下载的文件.txt 中,如下所示;

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

我的过程:

我尝试在 html 形式的一个文本区域中的每一行中进行多个输入。试图在 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);
    
    
    ?>
    

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

【问题讨论】:

  • 循环 $array 检查前六个字符的所有行

标签: php html


【解决方案1】:

您的代码的更正版本。内嵌评论。

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


?>

【讨论】:

  • 它工作正常,但问题是输出不在新行,它总是在同一行:(
  • 不,它在换行符中。这就是为什么我在 testid 之后添加了一个“\n”。请在文件写入后查看文件。此外,由于您发送的是文本文件,您可以将内容类型更改为“文本/纯文本”。您也可以相应地更改编码。
  • 如果我用这个就不行 ---> $contenter .= "My test name is {$testid} and {$testname}";
  • $testname 是您的整个 POST 输入。我想你的意思是别的。此外,您需要在字符串末尾添加一个换行符。
  • 你能更正完整的代码并更新它吗?我的编码不是很好...
【解决方案2】:

将此代码用于所需的结果

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

【讨论】:

    【解决方案3】:

    我使用file_put_contents,而不是fopenfclosefwrite,因为我个人之前遇到过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
    
    ?>
    

    【讨论】:

      【解决方案4】:
      $value = $request->get('testname1');
      $skuList = explode(PHP_EOL, $value);
      
      foreach ($skuList as $row)
      {   
          $testid = substr("{$row}",0,6);
          $contenter = "My testname is {$testid}";
      }
      

      【讨论】:

        【解决方案5】:

        试试下面的数组循环代码

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

        【讨论】:

          【解决方案6】:

          首先你拆分内容:

          $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 数据,而不是将其保存到文件然后输出。它是一样的,只是你不必为文件而烦恼。如果这样做,请确保在输出之前知道标头。

          【讨论】:

          • 我不是很好。你能把完整的php代码发给我吗?我还是迷路了:(
          • @ShahidShabbir 我已经更新了,请注意这是写在 SO 上的,甚至没有经过测试,但我认为它会起作用。
          • 错误:解析错误:语法错误,意外 '"' 正确。"' (T_CONSTANT_ENCAPSED_STRING),在第 16 行的 C:\xampp\htdocs\test2\xyz.php 中需要 ')'跨度>
          • @ShahidShabbir 嗯,这确实出乎意料。修复并更新了它。问题是1 无法与添加空格的字符串连接解决了它。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-02-27
          • 1970-01-01
          • 1970-01-01
          • 2019-06-27
          • 2017-12-01
          • 2011-03-23
          • 1970-01-01
          相关资源
          最近更新 更多