Putting file contents into a string or array.

Say you have a file called data.xml and you want to put it into a string. There are two ways I know off hand to do this. You can use the function file() or file_get_contents.

file() returns the contents as an array.

file_get_contents returns the contents as a string.

file() can also return the contents as a string if you use the implode() function.

First, here's how to return the contents of data.xml as a string -- both ways.

$data = file_get_contents('data.xml');

or

$data = implode("", file('data.xml'));

This last example just converts the array into a string via implode().

Next is returning the contents of data.xml as an array.

$a_data = file('data.xml');

My convention is to add a_ to the front of an array variable.

Also, there are other options like offsetting what is returned from the file. See the two links below for the syntax.

http://www.php.net/file » http://www.php.net/file-get-contents »

Page Comments (Click to edit)

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Powered by pmwiki-2.2.0-beta65