iamtom Posted September 6, 2010 Share Posted September 6, 2010 Thanks ahead of time, I have been working this all week end but get lost.... a. I want to insert all the lines from file1.txt into file2.txt . b. I want the all lines in file1.txt to be inserted in file2.txt starting at "<image>" tag label in file2.txt. c. I would like to overwrite all lines that start with the "<image>" tag in file2.txt with the insert (text in file1.txt contains <image> tag also). file2.txt has lines above and below the insert point <image>. The insert text (file1.txt) could be as many as twenty lines (records) all/each beginning with the<image>tag. Here is what I have done so far. <? $key = '<image>'; //a text tag in $file1 and $file2 //copy file $file = "file1.txt"; $file2= 'file2.txt"; $newfile = "filetemp.txt"; copy($file, $newfile) or exit("could not copy $file"); //load file array $fc = fopen ($file, "r"); while (!feof ($fc)) { $buffer = fgets($fc, 4096); $lines[] = $buffer; } fclose ($fc); //open same file and use "w" to clear file ,not sure this is needed $f=fopen($newfile,"w") or die("$file did not open"); /* uncomment to debug print_r($lines); print "<br>\n"; */ //loop through array 'foreach foreach($lines as $line) { fwrite($f,$line); //rewrite $line in file if (strstr($line,$key)){ //find $key in each line fwrite($f,$newline."\n"); } //rewrite $line in file } fclose($f); copy($newfile, $file2) or exit("could not copy $newfile"); ?> Again thanks ahead of time, I just keep losing the flow, old I guess. Tom Link to comment https://forums.phpfreaks.com/topic/212680-insert-lines-or-file-help/ Share on other sites More sharing options...
Adam Posted September 6, 2010 Share Posted September 6, 2010 I'm sorry but your description is a little confusing, could you give an example of the text files before and after? Link to comment https://forums.phpfreaks.com/topic/212680-insert-lines-or-file-help/#findComment-1107902 Share on other sites More sharing options...
iamtom Posted September 6, 2010 Author Share Posted September 6, 2010 Best to copy all into notepad to make more sense.... file2.txt <?xml version='1.0'?> <rss version='2.0'> <channel> <language>en</language><title></title> <link>http://www.375fss.com</link> <description>The latest news and updates from military leisure-time travel.</description> <docs>Mytest.pdf</docs> <generator>Tom Rector Weekend Wonders</generator> <pubDate>Sun, 05 Sep 2010 10:38:30 -0600</pubDate> <item> </channel></rss> file1.txt (watch line wrap , only two lines here) <item><author>Your Friendly Scott Tavel Agent</author><title>Rams Tickets</title><description>Purchase any Rams home game ticket(s) from Information, Tickets & Travel and receive an equal number of tickets FREE for the Rams home opener vs. Arizona Cardinals on Sept. 12. A limited number of FREE tickets are available (excludes Arizona Cardinals). The free home opener tickets are terrace level with a $40 value. Tickets can be picked up at the Scott Club\'s Football Fanatics Tailgate Event held Sept. 9 from 4 to 6 p.m. at the Scott Club. Anyone who purchases Rams tickets will automatically be entered to win Rams Autographed Items (names to be drawn at Tailgate party). For complete details call </description><link>http://www.375fss.com/RSSdata.xml</link><pubDate>Sun, 05 Sep 2010 10:02:16 -0600</pubDate></item> <item><author>Your Friendly Scott Tavel Agent</author><title>Rams Tickets</title><description>Purchase any Rams home game ticket(s) from Information, Tickets & Travel and receive an equal number of tickets FREE for the Rams home opener vs. Arizona Cardinals on Sept. 12. A limited number of FREE tickets are available (excludes Arizona Cardinals). The free home opener tickets are terrace level with a $40 value. Tickets can be picked up at the Scott Club\'s Football Fanatics Tailgate Event held Sept. 9 from 4 to 6 p.m. at the Scott Club. Anyone who purchases Rams tickets will automatically be entered to win Rams Autographed Items (names to be drawn at Tailgate party). For complete details call </description><link>http://www.375fss.com/RSSdata.xml</link><pubDate>Sun, 05 Sep 2010 10:38:30 -0600</pubDate></item> Final file (desired outcome) <?xml version='1.0'?> <rss version='2.0'> <channel> <language>en</language><title></title> <link>http://www.375fss.com</link> <description>The latest news and updates from military leisure-time travel.</description> <docs>Mytest.pdf</docs> <generator>Tom Rector Weekend Wonders</generator> <pubDate>Sun, 05 Sep 2010 10:38:30 -0600</pubDate> <item><author>Your Friendly Scott Tavel Agent</author><title>Rams Tickets</title><description>Purchase any Rams home game ticket(s) from Information, Tickets & Travel and receive an equal number of tickets FREE for the Rams home opener vs. Arizona Cardinals on Sept. 12. A limited number of FREE tickets are available (excludes Arizona Cardinals). The free home opener tickets are terrace level with a $40 value. Tickets can be picked up at the Scott Club\'s Football Fanatics Tailgate Event held Sept. 9 from 4 to 6 p.m. at the Scott Club. Anyone who purchases Rams tickets will automatically be entered to win Rams Autographed Items (names to be drawn at Tailgate party). For complete details call </description><link>http://www.375fss.com/RSSdata.xml</link><pubDate>Sun, 05 Sep 2010 10:02:16 -0600</pubDate></item> <item><author>Your Friendly Scott Tavel Agent</author><title>Rams Tickets</title><description>Purchase any Rams home game ticket(s) from Information, Tickets & Travel and receive an equal number of tickets FREE for the Rams home opener vs. Arizona Cardinals on Sept. 12. A limited number of FREE tickets are available (excludes Arizona Cardinals). The free home opener tickets are terrace level with a $40 value. Tickets can be picked up at the Scott Club\'s Football Fanatics Tailgate Event held Sept. 9 from 4 to 6 p.m. at the Scott Club. Anyone who purchases Rams tickets will automatically be entered to win Rams Autographed Items (names to be drawn at Tailgate party). For complete details call </description><link>http://www.375fss.com/RSSdata.xml</link><pubDate>Sun, 05 Sep 2010 10:38:30 -0600</pubDate></item> </channel></rss> Link to comment https://forums.phpfreaks.com/topic/212680-insert-lines-or-file-help/#findComment-1107925 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.