Using files

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
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Using files

Post 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.
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post 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.
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post 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?
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post 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;
Post Reply