deleting a line from a file

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
graziano
Forum Newbie
Posts: 9
Joined: Sat Dec 14, 2002 7:16 am

deleting a line from a file

Post by graziano »

Hello,

I have a user file user.php which contains following data

name1
name2
name3
name4
..
..


I am trying to write php code to open user.php , find "name2" (for example) , delete it , and save the file . Any idea to do that ?

thank you
Graz
graziano
Forum Newbie
Posts: 9
Joined: Sat Dec 14, 2002 7:16 am

Post by graziano »

please help ...
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

Try to explain it a bit better, i've read over and over, but i'm not quite getting it. Tell me exactly what you want to happen.

thanks
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

Here is one way to attack this.

Open the file in read mode.

Read each line and check to see if its the line you're looking for. If it's not, append it to an array. Repeat this till you get through the entire file.

Close the file.

Create a new file with the same name and send the contents of the array to the file and it will over write the old one.

Close the new file.

That's probably the long and slow way but it's late and I'm tired.
JP
Forum Newbie
Posts: 3
Joined: Tue Dec 24, 2002 3:13 am
Location: The Netherlands

deleting a line from a file

Post by JP »

Hi,

I've solve the problem by converting the textfile into an array.
(image gallery app @ http://xywel.net/CLOSE_ENCOUNTERS/)
You need to use a uniq separator character such as CR/LF (line end :D )
to be able to split the file content into array elements:

My file looks like:
filename_a.jpg TAB some description of this image CRLF
filename_z.jpg TAB some description of this image CRLF

which I split via:

Code: Select all

$content_array = spliti("ї\t]+|ї\n]+",$file_content);
This can be converted into an associative array, using filenames as a keys:

Code: Select all

$counter = 0;
while($counter < sizeof($content_array))
&#123;
$descriptions&#1111;$content_array&#1111;$counter]] = $content_array&#1111;$counter+1];
$counter = $counter + 2;
&#125;
Delete an array element via:

Code: Select all

unset ($descriptions&#1111;$key]);
Then rewrite the array as a file:

Code: Select all

fwrite($file_handle,$key."\t".$value."\n");
oh yeah chop the last \n on the last array element...

Cheers,

JP
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

nice. even i learnt something then. Well, thats not hard, i forget things as soon as i learn them. hahaha.
graziano
Forum Newbie
Posts: 9
Joined: Sat Dec 14, 2002 7:16 am

Post by graziano »

Thanks a lot !
Post Reply