Need help getting string variables into array

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
Summerschool23
Forum Newbie
Posts: 4
Joined: Tue Feb 08, 2005 8:09 pm

Need help getting string variables into array

Post by Summerschool23 »

Hello, I am working on a project involving php reading external files and I am having trouble figuring out a way to get variables in the external file into an array. The external file variables are like

Code: Select all

#var1:var1 text;
#var2:var2 text;
#var3:var3 text;
and I need to put just the values of the variables into an array looking like this (there are multiple files)

Code: Select all

&array=|var1 text,var2 text,var3 text|var1 text,var2 text,var3 text
any ideas...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your examples don't seem to correlate into a conversion logically.

you will need to create a parser for the external files. Depending on the flexibility you need, it may involve explode() and strpos() or regular expressions. It's possible you may use file() to break apart the file into seperate lines.. but that all depends on the language you create or have been dictated.


This smells like a school project. :?
Summerschool23
Forum Newbie
Posts: 4
Joined: Tue Feb 08, 2005 8:09 pm

Post by Summerschool23 »

I can get the file into a string using get_file_contents, but I don't know what to do from there. Maybe I'll try one of those functions, but I already have been looking through the php manual a lot and haven't had much luck.

This isn't actually for school, just a hobby project. I'm creating a flash game and this is part of it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

could you detail how the "language" works? This needs to be hard specifics.. once you have that, you can start down the path of creating a parser.
Summerschool23
Forum Newbie
Posts: 4
Joined: Tue Feb 08, 2005 8:09 pm

Post by Summerschool23 »

you mean the "language" as in the external file? That is just a widely used format for the game I am attepting to make. there are only about 5-15 variables, depending on what the author of the file wants in it. SOme variables are like

Code: Select all

#title:my title;
#creator:my username;
IDK if this is what you mean by the "language".
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is there a specification for this file format somewhere? Knowing how the files are laid out, we can better direct you to creating a parser.. or finding an existing one.

Which game?
Summerschool23
Forum Newbie
Posts: 4
Joined: Tue Feb 08, 2005 8:09 pm

Post by Summerschool23 »

:?
Post Reply