Hi bubble,
Is it going to work if I replace
' if(preg_match('/breakfast=cereal/',$data'
with a regular expression?
e.g. if(preg_match('/breakfast=(.*)/',$data
Because I will have no idea if I am going to have breakfast=taco
in the file.
say I have the text file containing:
breakfast=taco
lunch=taco
dinner= KFC
or
breakfast=cereal
lunch=taco
dinner= KFC
I wanted to have a function to be able to be smart enough to modify the breakfast.
BTW, that is really what I am having everyday. Tacos and KFC.
😉
Originally posted by bubblenut
<bubblenut voice="dodgy mexican">Oh yes</bubble>
first you need to open a handle on the file and one on a tmp file to write out to (slower if you're using small files but safer if you're using big files because the memory footprint is kept low)
$fh1=fopen($filename,'r');
$fh2=fopen('./tmp','w');
then read in the contents from it while searching for "breakfast"
while($data=fgets($fh1)) {
//if the line begins with "breakfast=cereal"
if(preg_match('/^breakfast=cereal/',$data)) {
$data="breakfast=burrito\n";
}
fwrite($fh2,$data);
}
//copy ./tmp over the original file
exec('mv ./tmp '.$file);
HTH
Bubble [/B]