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
deleting a line from a file
Moderator: General Moderators
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.
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.
deleting a line from a file
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
)
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:
This can be converted into an associative array, using filenames as a keys:
Delete an array element via:
Then rewrite the array as a file:
oh yeah chop the last \n on the last array element...
Cheers,
JP
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
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);Code: Select all
$counter = 0;
while($counter < sizeof($content_array))
{
$descriptionsї$content_arrayї$counter]] = $content_arrayї$counter+1];
$counter = $counter + 2;
}Code: Select all
unset ($descriptionsї$key]);Code: Select all
fwrite($file_handle,$key."\t".$value."\n");Cheers,
JP