Daithi Posted May 16, 2008 Share Posted May 16, 2008 Hi, I am new to the php world & my head is wrecked. I am trying to delete a line from a text file that contains mobile phone numbers. The mobile number to be deleted is passed onto the page from a dynamic form. I want to then take this mobile number search the text file for it & then delete it from the text file. Anything i have searched deletes the line number.... The text file is a list of mobile numbers 1 per line & no spaces. Please Help! Quote Link to comment Share on other sites More sharing options...
revraz Posted May 16, 2008 Share Posted May 16, 2008 You probably get more help if you... Post your current code Post a sample of the text file Also know, you can't just delete a line, you will need to read in the entire file, remove the parts that you don't want with PHP then re-write the new file. Quote Link to comment Share on other sites More sharing options...
Daithi Posted May 16, 2008 Author Share Posted May 16, 2008 thanks well even the logic behind it that you explained there helps, as i said i am new. from my reading up on it you have to - open the text file - copy contents of the text file to a new file except the line to be deleted - delete the initial file & rename the temporary file to what the initial one was ?? the text file is just... numbers.txt formatted like this. 1 number on 1 line. I am using one and just want to delete it from the list. 0861234567 0861234568 0861234569 0861234561 any more help, or even the correct logic how it happens & i can read up on the syntax, would be appreciated. Thank You. Quote Link to comment Share on other sites More sharing options...
kobmat Posted May 16, 2008 Share Posted May 16, 2008 1. open the text file using fopen in w+ mode 2.create a variable to temporarily storage of the line you are not looking for 3. read one line using fgets 4. compare the line contents with the data to be removed 5. if it is the one to be deleted skip that line, if its NOT add the line read to the temporary storage. 6. if its still not the end of file back to to set 3. use feof() 7. remove all the contents of the file using ftruncate() 8. write all the contents of the temp storage to the file Hope that helps Quote Link to comment Share on other sites More sharing options...
Daithi Posted May 16, 2008 Author Share Posted May 16, 2008 thank you... i'll give it a go. something to work off now. Quote Link to comment Share on other sites More sharing options...
Daithi Posted May 17, 2008 Author Share Posted May 17, 2008 Got that going this morning.... thanks again here it is it might help someone like me! <? $search = $_POST['used']; echo "The number $search has been used.<br><br>"; $file = "actn.txt"; $f = fopen($file, "r+"); $ammount = count(file($file)); // count lines in file $ammount--; for ($i=0; $i<=$ammount && $line = fgets($f, 1000); $i++) //place lines in an array except number that was used { if ($line != $search) $ctns[$i] = $line; } ftruncate($f,0); //delete file contents $ammount--; for ($i=0; $i<=$ammount; $i++) // write array to text file { $stringData = $ctns[$i]; fwrite($f, $stringData); } fclose($f); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.