在那里,我试图创建一个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" /> data1: <input type="text" name="mail" value="0" size="1" /> data2: <input type="text" name="pwd" value="1" size="1" />
<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
但是它保存了所有数据,请帮助
答案 0 :(得分: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" /> data1: <input type="text" name="mail" value="0" size="1" /> data2: <input type="text" name="pwd" value="1" size="1" />
<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);
}
?>