Jump to content

Insert lines or file help


iamtom

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.