Page 1 of 1

PHP Line Delete Help

Posted: Fri Nov 26, 2004 7:45 pm
by Pandemic!
Hello, I have a php script to delete a line in a file, but it seems to spread all the lines out in the file that it is supposed to be editing, can someone please help me or just give me a script for what I need.

-Thnx :)


<?php
$line_to_delete = 3;
$filename = 'joinstuff.php';
$file_data = file($filename);
unset($file_data[$line_to_delete]);
$file_pointer = fopen($filename,'w');
$new_file_data = implode("\n",$file_data);
fwrite($file_pointer,$new_file_data);
fclose($file_pointer);
?>

All it basically does it take a file that looks liek this:

xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx

and turns it into this:

xxxxxxxxxxxx

xxxxxxxxxxxx

xxxxxxxxxxxx

xxxxxxxxxxxx

Re: PHP Line Delete Help

Posted: Fri Nov 26, 2004 7:48 pm
by timvw

Code: Select all

$new_file_data = implode("\n",$file_data);
You ask to add a newline... and then you wonder why there is a newline added? :p

Posted: Fri Nov 26, 2004 7:49 pm
by Pandemic!
Eep! Thanks, but how would I fix it? (Sorry I am new at php)

edit: I asked?.. no, this isnt my code, I just got it from a tutorial, but I didnt really know how it worked, but I see that /n puts a new line... so how would I make it so that it removes the line I want it to?

Posted: Fri Nov 26, 2004 7:55 pm
by timvw
why do you call a function if you don't have a clue what it is doing?

in the manual, the section on fread -> http://www.php.net/fread there is a user comment on how to read a file line by line...

all you would have to do is count (each line, counter = counter + 1) and when it equals 3 it should do nothing, otherwise it should write the line to the newdata...

Posted: Fri Nov 26, 2004 7:58 pm
by Pandemic!
:( i just ask for some help one fixing the code... I like just started using php... I would just like to get some help... is it so hard?

Posted: Fri Nov 26, 2004 8:17 pm
by rehfeld
$new_file_data = implode("",$file_data);

Posted: Fri Nov 26, 2004 8:18 pm
by Pandemic!
I tried that, but then it just didnt do anything..

Posted: Fri Nov 26, 2004 8:40 pm
by rehfeld
then you did it wrong. try it again.

remember, to delete the first line in the file, you would do

$line_to_delete = 0;

Posted: Fri Nov 26, 2004 8:43 pm
by Pandemic!
i think i get it now, so say i have line 10 in notepad, i would have the script at line 9?

cause line 1 in notepad is like 0 in php?

Posted: Fri Nov 26, 2004 8:46 pm
by Pandemic!
yes! thank you so much man, it works :)