Current location: Hot Scripts Forums » Programming Languages » PHP » [PHP] Remove line in file


[PHP] Remove line in file

Reply
  #1 (permalink)  
Old 12-12-07, 02:07 PM
lylesback2 lylesback2 is offline
Wannabe Coder
 
Join Date: Dec 2003
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
[PHP] Remove line in file

I'm working on a file system (thanks to help from programmingTalk) and now i'm stuck on deleting a selected line from a file.

Say I wanted to delete line 4, out of 23, how can I go about doing that?
So far the code I have will display which lines in an array, that it wants to delete, and is already in a for loop
PHP Code:

if(isset($_POST['Band'])) {
    for (
$i=0$i<count($_POST['Band']);$i++) {

       
$LineNumber $_POST['Band'][$i];
       echo 
$LineNumber;

    }

so far that will only display what is selected (the echo was just to see if it works)
Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-12-07, 02:14 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
You will have to suck the file into an array, probably easiest with file().

Then you can either remove the value you'd like zapped and implode() the contents back into the file, or rewrite the file and skip over the unwanted line with a blank space.

Also, after removing the line, will the unwanted line be left as an open empty line, or just removed altogether? So with 23 original lines, will removing line #4 leave 23 lines with a blank 4th line... or 22 full lines?
__________________
The toxic ZCE
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-12-07, 02:30 PM
lylesback2 lylesback2 is offline
Wannabe Coder
 
Join Date: Dec 2003
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Oh, the line number will be removed all together, making it appear as if nothing was ever modified.

I read some guides though google that said to take the lines I want, skip the line(s) being deleted, and write to file...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-12-07, 04:37 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Here's a quick and dirty example that should do what you'd like:
PHP Code:

<?php

// The file
$filePath './file.txt';

// Grab file into an array, by lines
$fileArr file$filePath );

// Remove desired line
unset( $fileArr[3] ); // $fileArr[3] == line #4

// Put back with PHP4
$success FALSE;
if ( ( 
$fp fopen$filePath'w' ) ) !== FALSE )
{
    if ( 
flock$fp) ) // LOCK_EX
    
{
        
fwrite$fpimplode''$fileArr ) );
        
flock$fp); // LOCK_UN
        
$success TRUE;
    }
    
fclose$fp );
}

/*
// Or, put back with PHP5
$success = FALSE;
if ( file_put_contents( $filePath, implode( '', $fileArr ), LOCK_EX ) )
{
    $success = TRUE;
}
*/

?>
__________________
The toxic ZCE

Last edited by Keith; 12-12-07 at 04:44 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
No error message... stormshadow PHP 3 12-11-06 07:31 PM
PHP - Unable to remove items from a file AdrianLewis PHP 5 03-17-06 06:14 AM
Redirection back to a page from form submit DAL Perl 11 03-21-05 03:45 PM
I most definately suggest DevelopingCentral.com For Any Website Design/Development! Salty777 General Advertisements 2 10-01-04 05:27 AM
asp-iis-Server error nsuresh_rasr ASP.NET 3 02-08-04 01:47 AM


All times are GMT -5. The time now is 12:12 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.