Jump to content


Photo

Deleting row in text file?


  • Please log in to reply
3 replies to this topic

#1 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 20 July 2006 - 08:30 PM

Hello,

Currently, I need to create a script that deletes a select row from a text file, at each \n character, that would mark a row...

Now, let's say I have this for information... (in a text file)

text|somemoretext|finaltext
ytext|ysomemoretext|yfinaltext

I'd want my script to display that, which it does, but I'd also want the row number returned to the script, so it would know which row to delete.

Any ideas how I'd do this? I could just use write, and re-write all the rows updated to the file, but if there is a way to just delete a row, then I'd like to take that path.

Thanks.

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 July 2006 - 08:32 PM

try the file() command, which will read the entire file into an array, with each row in it's own record.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 Kurt

Kurt
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 20 July 2006 - 08:34 PM

<?php
$content=file_get_contents("file.txt");
$rows=explode("\n",$content);
?>

Now, every row is in the $rows array. So, to access the first row you would do $rows[0]. To access the second row you would do $rows[1]. And, any other row after that, you know what to do.

#4 AncientSage

AncientSage
  • Members
  • PipPipPip
  • Advanced Member
  • 56 posts

Posted 20 July 2006 - 09:17 PM

'k, thanks.

Now, I have one more question...

In a foreach loop, is it possible to exclude the last row? (Which is, blank.)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users