PHP Line Delete Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pandemic!
Forum Newbie
Posts: 6
Joined: Fri Nov 26, 2004 7:41 pm

PHP Line Delete Help

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: PHP Line Delete Help

Post 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
Pandemic!
Forum Newbie
Posts: 6
Joined: Fri Nov 26, 2004 7:41 pm

Post 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?
Last edited by Pandemic! on Fri Nov 26, 2004 7:56 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
Pandemic!
Forum Newbie
Posts: 6
Joined: Fri Nov 26, 2004 7:41 pm

Post 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?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

$new_file_data = implode("",$file_data);
Pandemic!
Forum Newbie
Posts: 6
Joined: Fri Nov 26, 2004 7:41 pm

Post by Pandemic! »

I tried that, but then it just didnt do anything..
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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;
Pandemic!
Forum Newbie
Posts: 6
Joined: Fri Nov 26, 2004 7:41 pm

Post 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?
Pandemic!
Forum Newbie
Posts: 6
Joined: Fri Nov 26, 2004 7:41 pm

Post by Pandemic! »

yes! thank you so much man, it works :)
Post Reply