-3

您好,我正在尝试创建一个 php 脚本来提取问题之间的数据并将其保存在文本文件中

数据是这样的

data 0 | Data 1 | data 2 | data 3 | data 4 | data 5 | data 6 
data 0 | Data 1 | data 2 | data 3 | data 4 | data 5 | data 6 

这是我正在使用的脚本

<html>
<body>
    <form method="post">
<div align="center"><textarea name="mp" cols="60" rows="10">0|1|2</textarea><br />

Delim: <input type="text" name="delim" value="|" size="1" />&nbsp;data1: <input type="text" name="mail" value="0" size="1" />&nbsp;data2: <input type="text" name="pwd" value="1" size="1" />&nbsp;
<input type="submit" value=" send " name="btn-submit" />
</div>
</form>
</body>
</html>
<?php
if(isset($_POST['mp'])) { //only do file operations when appropriate
    $a = $_POST['mp'];
    $myFile = "t.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $a);
    fclose($fh);
}
?>    

我希望它基本上抓取我选择的数据,所以如果我选择 dilem 作为|第 3 节和第 5 节,它应该像这样保存数据data 3:data 5

但它正在保存所有数据,请帮忙

4

1 回答 1

0

我不能确定,但​​我认为这就是你要找的:

     <html>
  <body>
      <form method="post">
  <div align="center"><textarea name="mp" cols="60" rows="10">0|1|2</textarea><br />

  Delim: <input type="text" name="delim" value="|" size="1" />&nbsp;data1: <input type="text" name="mail" value="0" size="1" />&nbsp;data2: <input type="text" name="pwd" value="1" size="1" />&nbsp;
  <input type="submit" value=" send " name="btn-submit" />
  </div>
  </form>
  </body>
  </html>
  <?php
  if(isset($_POST['mp'], $_POST['delim'], $_POST['btn-submit'])) { //only do file operations when appropriate
      // separate the value into an array of newlines
      $delim = $_POST["delim"];
      $lines = explode("\n", $_POST["mp"]);
      // get the indexes that have the data you want
      $email = $mp[$_POST["mail"]];
      $password = $mp[$_POST["pwd"]];
      // open the file
      $myFile = "t.txt";
      $fh = fopen($myFile, 'w') or die("can't open file");
      foreach ($lines as $line){
        $row = str_getcsv($lines, $delim);
        fputs($fh, $rec[$email].":".$rec[$password]."\n");
      }
      fclose($fh);
  }
  ?>    
于 2013-07-16T16:28:02.190 回答