Howto write an array to a file

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
User avatar
soren
Forum Newbie
Posts: 14
Joined: Wed Oct 16, 2002 2:36 pm
Location: Denmark

Howto write an array to a file

Post 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
User avatar
horgh
Forum Newbie
Posts: 23
Joined: Mon Oct 21, 2002 9:50 am
Location: GER
Contact:

Post 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()
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

I like using the implode, then explode to get it out of the array

works like a chizzarm
Post Reply