Creating array with text file contents

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
LukeSquall
Forum Newbie
Posts: 2
Joined: Fri Feb 20, 2004 3:23 pm

Creating array with text file contents

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

explode() function might help
LukeSquall
Forum Newbie
Posts: 2
Joined: Fri Feb 20, 2004 3:23 pm

Post by LukeSquall »

Thanks man, that helps a lot!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post 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.
Post Reply