can you explain what is happening here?
for example:
what is $f[2]'something else' ? is it another file?
and if I want to replace AppParam=this with AppParam=that what is it i use?
Main Topics
Browse All TopicsIm trying to fopen a file so that i can replace a line while leaving the other lines alone.
Example:
I want to fopen C:\test.txt which consists of:
Line1 = Ip address
Line2 = Port
Line3 = parameters
I want to be able to replace Line 3 with new parameters while leaving line 1 and line 2 alone. What do i use to do this?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Basically you read the file into an array line-by-line, change the 3rd element of the array to what you want, turn this back into a string and write it back to the file.
I'll go through the code line by line here
//file reads the entire contents of the file into an array( see http://us3.php.net/file ) line by line, leaving the \n in place
$f=file('c:\test.txt');
//so now replace the third line with your new text
$f[2]='something else';
//now open the file, the w tells your it's for writing ( see http://us3.php.net/fopen )
$o=fopen('c:\test.txt', 'w');
//fwrite takes a string as an arg and writes it to the file ( http://www.php.net/fwrite )
//implode puts the elements of the given array into a string with the first argument seperating them
//( http://us2.php.net/implode
//note that since the \n is still there when you read the file into the array, so you don't need a seperator
fwrite($o, implode("", $f));
//close the file
fclose($o);
ok well the orginal test.txt looks like this:
Line1
AppParm=hello world
Line3
Line4
now im using yuor code:
<?
$f=file('/home/supervise/t
$f[1]='AppParm=this and that';
$o=fopen('/home/supervise/
fwrite($o, implode("", $f));
fclose($o);
?>
and it works almost perfectly. its cahning it to this:
Line1
AppParm=this and thatLine3
Line4
Notice that its not line breaking after line 2. What can I do so it changes it to:
Line1
AppParm=this and that
Line3
Line4
Thanks
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:
Split: Arantius, Saifatlast, kl4518
Any objections should be posted here in the next 4 days. After that time, the question will be closed.
- Neester -
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: arantiusPosted on 2005-10-16 at 12:06:52ID: 15095346
<?
$f=file('c:\test.txt');
$f[2]='something else';
$o=fopen('c:\test.txt', 'w');
fwrite($o, implode("\n", $f));
fclose($o);
?>