Page 1 of 1

Howto write an array to a file

Posted: Mon Dec 02, 2002 2:05 pm
by soren
What is the best way to write an array to a text/html file ?

I've written a short script, which loads an html file into an array, runs through it removing all <something> tags and now need to save it as a different file.

Should I use fopen, tempnam or ?

Soren O'Neill

Posted: Mon Dec 02, 2002 3:09 pm
by horgh
u should use serialize() to make storable data of that array or u just use foreach() with every line in that array and then u put it into a new file with fopen()

Posted: Tue Dec 03, 2002 6:51 am
by twigletmac
You can also use implode() to convert the array to a long string.

From the user notes on implode():
$file_text = implode('', file('data.htm'));

slurps file data.htm into variable $file_text unchanged (vs the file content). Newlines are all in the right place because newlines were the last char in each array element. That's how file() splits the input.
Mac

Posted: Tue Dec 03, 2002 10:38 am
by JPlush76
I like using the implode, then explode to get it out of the array

works like a chizzarm