如何在php中打开文件并将字符串放在每行的末尾
请告诉我如何在php中打开文件并将字符串放在每行的末尾 这可能吗 谁能让我明白这个密码如何在php中打开文件并将字符串放在每行的末尾,php,file,Php,File,请告诉我如何在php中打开文件并将字符串放在每行的末尾 这可能吗 谁能让我明白这个密码 f = open('./ampo.txt', 'r+') with open('./ampo.txt') as infile: for line in infile: f.insert(0, 'EDF ') f.close 这可能是你想要的 $myFile = "testFile.txt"; // The file $fh = fopen($myFile, 'a') or die(
f = open('./ampo.txt', 'r+')
with open('./ampo.txt') as infile:
for line in infile:
f.insert(0, 'EDF ')
f.close
这可能是你想要的
$myFile = "testFile.txt"; // The file
$fh = fopen($myFile, 'a') or die("can't open file"); // Open it
$stringData = "New Stuff 1\n"; // Your data
fwrite($fh, $stringData); // Write to file
$stringData = "New Stuff 2\n"; // Your data
fwrite($fh, $stringData); // Write again
fclose($fh); // Close it
fopen中的A(append)将确保将内容附加到文件,以便附加到文件中的每一行,您必须读取文件,然后覆盖它。实际上,我建议您写入另一个文件,如果您选择这样做,只需在fopen调用中更改文件名即可
$lines = file("./amp.txt");
$fp = fopen("./amp.txt", "w");
foreach($lines as $line) {
fwrite($fp, substr($line,0,-1) . "EDF" . substr($line,-1));
}
fclose($fp);
在我看来,使用
'a+'
而不是'r+'
看起来像Python,这一点都不过分。。。