Page 1 of 1

Creating array with text file contents

Posted: Fri Feb 20, 2004 3:23 pm
by LukeSquall
Hi,

I would like to get the contents from a text file, and add certain peices of data to an array. I don't have a problem with reading the text file, but I can't work out how to seperate the peices of data in the text file. I thought maybe I could create a text file containing the following:

"DY","LS",User3"

And adding the following code to my PHP script:

Code: Select all

<?php
$filename = "users.txt";
$file = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($file);

$users = array($contents);
echo($users&#1111;0]);
?>
But it just displays the whole of the textfile. I would appreciate if someone could come up with a solution,
Thanks, luke.

Posted: Fri Feb 20, 2004 3:34 pm
by d3ad1ysp0rk
explode() function might help

Posted: Fri Feb 20, 2004 3:41 pm
by LukeSquall
Thanks man, that helps a lot!

Posted: Fri Feb 20, 2004 4:50 pm
by pickle
Sounds like you have the ability to change the format of the text file. You might want to consider putting each entry on a separate line, then use file() to open the file. file() reads each line into an array - which saves you the trouble of having to do it.

Posted: Fri Feb 20, 2004 10:03 pm
by evilMind
I say store the data serialized. then all you have to do is read the file into a string then say $foo = unserialize($stringContentsOfFile);

see [php_man]unserialize[/php_man] for more info.