views:

490

answers:

4

I had no idea to correctly form the title of this question, because I don't even know if what I'm trying to do has a name.

Let's say I've got an external file (called, for instance, settings.txt) with the following in it:

template:'xaddict';
editor:'true';
wysiwyg:'false';

These are simple name:value pairs. I would like to have php take care of this file in such a way that I end up with php variables with the following values:

$template = 'xaddict';
$editor = 'true';
$wysiwyg = 'false';

I don't know how many of these variables I'll have.

How should i go and do this?

The data inside the file is in simple name:value pairs. No nesting, no complex data. All the names need to be converted to $name and all the values need to be converted to 'value', disregarding whether it is truly a string or not.

+2  A: 
$settings = json_decode(file_get_contents($filename));

assuming your file is in valid JSON format. If not, you can either massage it so it is or you'll have to use a different approach.

cletus
this way I've got to create a valid JSON file, which means introducing the {} tags. I'd like to stay away from it.
xaddict
A: 

If you use JSON, you can use something like:

extract(json_decode(file_get_contents('settings.json')));

Using extract may be dangerous, so I suggest to store these settings in an array:

$settings = json_decode(file_get_contents('settings.json'));
danielrmt
this would create the $settings["template"] variable, but I'm trying to create the variable $template. I don't care much about JSON, so if it can be done without... gladly
xaddict
Then use the extract function. It can create a security hole, but it does what you want.
danielrmt
If you don't want to use {} in your file, you can mimic a fake json with extract(json_decode('{'.file_get_contents('settings.json').'}'));
danielrmt
A: 

You should read your file to an array, with the file() function, then you should cycle on it: for each line (the file() function will return an array, one line per item), check if the line is not blank, then explode() on the ":" character, trim the pieces, and put them into an array. You will end up win an array like this:

[template] = xaddict
[editor] = true

then you can use this information.

Do not automatically convert this into local variables: it's a great way to introduce security risks, and potentially very obscure bugs (local variables obscured by those introduced by this parsing).

Palantir
is variable variables a solution? like this:$a = 'template';$$a = 'xaddict';echo "$template";returns 'xaddict'
xaddict
+1  A: 

Do you want 'true' in "editor:'true'" to be interpreted as a string or as a boolean? If sometimes string, sometimes boolean, how do you know which?

If you have "number='9'" do you want '9' interpreted as a string or an as an integer? Would '09' be a decimal number or octal? How about "number='3.14'": string or float? If sometimes one, sometimes the other, how do you know which?

If you have "'" (single quote) inside a value is it escaped? If so, how?

Is there any expectation of array data?

Etc.

Edit: This would be the simplest way, imo, to use your example input to retrieve your example data:

$file = 'test.csv';
$fp = fopen($file, 'r');
while ($line = fgetcsv($fp, 1024, ':')) {
    $$line[0] = $line[1];
}
GZipp
the only stuff in the file will be strings ('xaddict'), numbers ('9' or '9.16') and true/false text values. I'll try to implement a way to turn all values into the ones I desire, but that's not the problem right now. of later concern. Probably I'll turn all numbers into doubles (floats), whether they're decimals or not.
xaddict
plus. I found out in php: '9.126' == 9.126. returns true.
xaddict
Yes, in that simple case it works fine.
GZipp
this is perfect. Should've thought about Comma Separated Values in the first place. Thanks bunches!!!
xaddict
I'm glad it helped. For the record, though, my questions were intended to nudge you toward considering something like JSON instead. :)
GZipp