Edit a specific line of a file???

This is a discussion on "Edit a specific line of a file???" within the PHP Forum section. This forum, and the thread "Edit a specific line of a file??? are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 8th, 2008, 23:53
Junior Member
Join Date: Sep 2006
Location: Here
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Edit a specific line of a file???

Is there a way to edit a certain line in a file with PHP?

Maybe use fopen() and insert new POST data into, say, line 5 replacing what's there?
Reply With Quote

  #2 (permalink)  
Old Feb 9th, 2008, 00:06
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Edit a specific line of a file???

Maybe this:
PHP: Select all

// put contents of file into $file forehand
$lines explode("\n",$file);
$lines[4] = "new line 5"//index is zero based so 4 is actually line 5
$file implode("\n"$lines);
//write $file to the file 
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #3 (permalink)  
Old Feb 9th, 2008, 00:53
Junior Member
Join Date: Sep 2006
Location: Here
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Edit a specific line of a file???

Like this?
PHP: Select all

<?php
$file 
fopen("template/templateHeader.inc""r");


$lines explode("\n",$file);
$lines[4] = "new line 5";
$File implode("\n"$lines);

fputs($file"$file");
fclose($file);
?>
Reply With Quote
  #4 (permalink)  
Old Feb 9th, 2008, 01:35
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Edit a specific line of a file???

This should work:
<font color="#000000">
PHP: Select all

<?php
$filehandle 
fopen("template/templateHeader.inc""r");
$file fread($filehandle);
$lines explode("\n",$file);
$lines[4] = "new line 5";
$file implode("\n"$lines);

fputs($file"$file");
fclose($file);
?>
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
  #5 (permalink)  
Old Feb 10th, 2008, 05:03
CloudedVision's Avatar
Nerdy Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 942
Blog Entries: 8
Thanks: 2
Thanked 22 Times in 22 Posts
Send a message via AIM to CloudedVision Send a message via MSN to CloudedVision Send a message via Skype™ to CloudedVision
Re: Edit a specific line of a file???

Shouldn't it be \r\n instead of just \n? Just \n has never worked for me.
Last Blog Entry: More Cheat Sheets (Jul 12th, 2008)
Reply With Quote
  #6 (permalink)  
Old Feb 10th, 2008, 11:50
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
Re: Edit a specific line of a file???

Depends what OS you are on.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
reading a log file one line at a time depending on timestamp eon201 PHP Forum 1 Dec 4th, 2007 05:54
Edit one file, that changes the column on all pages. Dark[Devil] Web Page Design 16 Aug 25th, 2007 20:47
Loading specific flash movies for specific pages koonkle Flash & Multimedia Forum 3 May 22nd, 2007 17:23
Trying To Edit A Template But Missing psd File...Please Help fst2006 Web Page Design 5 Nov 18th, 2006 18:50
display folder contents + specific text for a particular file type magnum Classic ASP 1 Aug 17th, 2006 13:02


All times are GMT. The time now is 20:49.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43