1

I'm trying to parse basically an ini file without using parse_ini_file for a few reasons. I have the file parsing perfectly into a multidimensional array and am able to search through it properly and everything. The problem that I am having, is that I want the file to be parsed based on the type. There are strings, floats and integers in the file and I need to store these as such rather than all as strings.

The users will need to be able to get the values stored, so basically they would do "Get Float from Header section with the Budget key" and this would return 4.5. However if they tried to get the string or integer from the same section it would fail. I have it able to get the keys, but since they are all stored as strings that is the only thing that works. I'm wondering what how to change the type of the value from string to integer or float.

Anyone have any ideas how to do this?

3
  • 2
    What are the reasons you can't just use parse_ini_file for this? As for being able to parse the type, you're probably going to have to encode the type data into the string. For example, foo=string(bar) or similar. If the data is getting that complex I'd really suggest you use one of PHP's file parsers (ini, json, xml, etc) or a database of some sort. Commented Mar 7, 2013 at 23:43
  • Is this a standard ini file or are you constructing it yourself? Commented Mar 7, 2013 at 23:45
  • It isn't formatted like a standard ini file and throws errors when trying to use it, due to special characters. Commented Mar 7, 2013 at 23:56

2 Answers 2

1

It would be very difficult to accurately predict what type of value you are looking at. Strings could contain numbers, decimal points appear in sentences, etc...

If you are dealing with a custom ini file that you are generating for your own use, you might be able to encode the type of value into the ini settings. A standard setting may look like this -

default_username=Zamereon

So you could append or prepend the data type to the setting -

(s)default_username=Zamereon // one character depicting the data type,
(i)default_reputation=1      // you could use strpos.

default_balance=0.5=f // use list($name,$value,$type) = explode('=',$setting)

References -


Another suggestion would be to leave them as strings in your parsed multidimensional array and only convert them when you actually need them. Within a function or line of code dealing with some inputted data from the ini file, you'll know exactly what data type you'll need, if it's a float just cast it to a float as you need it. There's not real need to cast every variable to it's correct form right away because their value will not change in it's textual representation; The conversion only truly needs to take place before performing some further manipulations.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use type casting

Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast.

<?php
$foo = 10;   // $foo is an integer
$bar = (boolean) $foo;   // $bar is a boolean
?>

The casts allowed are:

(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object
(unset) - cast to NULL (PHP 5)

1 Comment

How will I determine what value type it is? It needs to apply them dynamically as the users can change the values of the file. So when reading in the file it will need to read it and determine if it should be an int, string or float.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.