Page 1 of 1

Using files

Posted: Tue Apr 13, 2004 3:52 pm
by reverend_ink
I dont often use files, but what I need to do is open, read and right to a file which I use w+ for, but thats not the question.

The question is I need to be able to open the file, parse the data so I can display it as an array, then edit or delete sections of the file.

Any thoughts or suggestions?

Much appreciated.

Posted: Tue Apr 13, 2004 4:10 pm
by RadixDev
Use file() function if you want to read line by line so...

Code: Select all

<?php
$file = file("stuff.php"); // No need for fopen I don't think..
foreach($file as $line_num => $line_data){
  // Do you stuff
}
?>
Use str_replace() to change stuff in the file. Check out the manual for details.

Posted: Tue Apr 13, 2004 5:34 pm
by reverend_ink
RadixDev wrote:Use file() function if you want to read line by line so...

Code: Select all

<?php
$file = file("stuff.php"); // No need for fopen I don't think..
foreach($file as $line_num => $line_data){
  // Do you stuff
}
?>
Use str_replace() to change stuff in the file. Check out the manual for details.
This will work with text files too wont it?

And a "[" can be removed from the file contents as well I assume?

Posted: Tue Apr 13, 2004 5:58 pm
by RadixDev
Yep file() works with any kinds of files.
to replace [ with something different (space):

Code: Select all

&lt;?php
$file = file("stuff.txt");
$newfile = "";
foreach($file as $line_num =&gt; $line_data){ 
  $newfile .= str_replace("&#1111;"," ",$lin_data);
} 
?&gt;