2016-03-14 49 views
0

我想从数组中追加文本到用户选定的文件行的末尾。 Intro to PHP$$Prog-6218$$7 Java 3$$Prog-2743$$4$$ Linux$$Prog-5490$$8$$ Online Marketing$$Buis-5567$$10$$ Marketing History$$Buis-2247$$9$$ World Impact of Computing$$Elect-7785$$5$$行重写int txt文件php

如何我无法弄清楚如何覆盖所有线路的线路

+0

看起来像一些人需要学习如何使用一个真正的数据库 – 2016-03-14 23:03:56

+0

现在我只是一个初学者试图做一些文件处理我不熟悉的语言。到目前为止,我只能使用一个数据库只有Java,但我正在尝试 – user3316257

+0

它实际上比平面文件更少代码,并且比平面文件更容易 – 2016-03-14 23:10:34

回答

0

下面是你会怎么做速写:

$fname = "books.txt"; 
$insertLine = 3; 
$newLine = "Code Complete$$Prog-1234$$9$$"; 

// Read lines from file into array and split by new lines 
$lines = explode(PHP_EOL, file_get_contents($fname)); 

// Insert your new line/book details 
array_splice($lines, $insertLine, 0, $newLine); 

// Glue old + our new line back into array using 
// new line as element separator and write back to file 
file_put_contents($fname, implode(PHP_EOL, $lines)); 

我注意到你的线条是由$$分隔。你可能想看看str_getcsv()fputcsv()