我是PHP新手,仍然在学习它。这次我想打印一些文本并保存在文件中。但我希望自动更改文件名。
例如file1.txt,file2.txt,file3.txt等。 到目前为止,这是我的代码:
$read = file("example.txt",FILE_IGNORE_NEW_LINES);
$newFile = "file_1.txt";
$of = fopen($newFile,'w');
foreach($read as $k => $file){
$line = explode(" ",$file);
$line2 = array_slice($line,0,3);
foreach($line2 as $value){
echo $value." ";
fwrite($of,$value." ");
}
echo "<br>";
fwrite($of,"\n");
}
fclose($of);
答案 0 :(得分:0)
$read = file("example.txt",FILE_IGNORE_NEW_LINES);
$filesToWrite = ["file_1.txt", "file_2.txt","file_3.txt"];
foreach($filesToWrite as $fileToWrite){
$of = fopen($fileToRead,'w');
foreach($read as $k => $file){
$line = explode(" ",$file);
$line2 = array_slice($line,0,3);
foreach($line2 as $value){
echo $value." ";
fwrite($of,$value." ");
}
echo "<br>";
fwrite($of,"\n");
}
}
fclose($of);
我希望能帮到你!