Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中打开文件并将字符串放在每行的末尾_Php_File - Fatal编程技术网

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

请告诉我如何在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("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,这一点都不过分。。。