473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to parse a config file into an array?

Ok,

I have tried, and googled and this is a last resort.

I have a config file like so:
Expand|Select|Wrap|Line Numbers
  1. //comment
  2. variable = "value" //for text
  3. variable = value //numeric
  4.  
I have managed to get php to output an array like so (using the explode function)
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.      [0] => variable = "value"
  4. )
  5.  
  6. and not ...
  7.  
  8. Array
  9. (
  10.      [ variable] => "value"
  11. )
How do I do this?

Many Thanks!
Sep 12 '07 #1
15 3162
code green
1,726 Expert 1GB
[PHP]//Declare variables
$t_variable = "value" //for text
$v_variable = 1 //numeric[/PHP]Populate the array
[PHP]$arry = array();
$arry[$t_variable] = $v_variable;[/PHP]or you can try
[PHP]$arry = array($t_variable=>$v_variable);[/PHP]
Sep 12 '07 #2
Ah, but the config is in a file called config.cfg
Sep 12 '07 #3
ronverdonk
4,258 Expert 4TB
Ah, but the config is in a file called config.cfg
You lost me there! Could you please rephrase the problem? As I understand it now you have a file and you want to move its content into an array(?).

Ronald
Sep 12 '07 #4
Ok,

I have a file called server.cfg which contains:

//comments
servername = "myserver"
port = 1234

An then I want my PHP to break that down into variables (I have been using Arrays there maybe a better way).

$servername =

So that I can create a load of input boxes from the config to make it easy to edit.

Thanks!
Sep 12 '07 #5
ronverdonk
4,258 Expert 4TB
Putting it in an array (here done with the split function) can be done as follows, assuming that you have a 'clean' input line, e.g. comments and the likes taken out
[php]
list($key,$val) = split('=', $inputline);
$array[$key]=$val;
[/php]

Ronald
Sep 12 '07 #6
kovik
1,044 Expert 1GB
Take a look at parse_ini_file().
Sep 12 '07 #7
ronverdonk
4,258 Expert 4TB
Take a look at parse_ini_file().
You are so right! In my drive to populate an array via code I completely overlooked the most obvious solution. Thanks.

Ronald
Sep 12 '07 #8
Atli
5,058 Expert 4TB
I've changed the title of this thread to better describe it's topic.
Using good, descriptive titles that follow the Posting Guidelines will increase your chances of getting you questions answered!

Moderator
Sep 12 '07 #9
Yeh, I tried "parse_ini_file()" but it was a bit crasy.
Sep 12 '07 #10
Expand|Select|Wrap|Line Numbers
  1. <? 
  2.  
  3. $url = ('server.cfg');
  4.  
  5. $fh = fopen($url, 'r');
  6. $file_cfg = fread($fh,10000);
  7. fclose($fh);
  8.  
  9. list($key,$val) = split(' ', $file_cfg);
  10. $array[$key]=$val;
  11.  
  12. print_r($array['servername']
  13.  
  14. ?>
  15.  
That only gets the first line.

How Do I get all of them?
Sep 12 '07 #11
BTW I got rid of the '=' the values are split by an ' '
Sep 12 '07 #12
ronverdonk
4,258 Expert 4TB
Do the file reading in a loop. Like this:
[php] $fh = @fopen($url, "r");
while (!feof($fh)) {
$file_cfg = fgets($fh);
list($key,$val) = split(' ', $file_cfg);
$array[$key]=$val;

}[/php]
Ronald
Sep 12 '07 #13
pbmods
5,821 Expert 4TB
Heya, Hugo.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]

parse_ini_file() looks like it will do exactly what you're looking for. Pay special attention to the process_sections parameter.
Sep 12 '07 #14
I'll take another look
Sep 13 '07 #15
Do the file reading in a loop. Like this:
[php] $fh = @fopen($url, "r");
while (!feof($fh)) {
$file_cfg = fgets($fh);
list($key,$val) = split(' ', $file_cfg);
$array[$key]=$val;

}[/php]
Ronald

W00t thanks your pro :p
Sep 13 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Charlie Smith | last post by:
Hello All I'm writing a little app for my personal use and want to store the configuration data in an xml file. I found PEAR::Config that lets me read the config data into my app. But so far,...
2
by: iop | last post by:
Hello there, I'd like to "parse" an entire multi-dimension array like this : APP APP without knowing "framework" or "config" or anything passed as variables... 'cause it's simple to call...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
21
by: William Stacey [MVP] | last post by:
Anyone know of some library that will parse files like following: options { directory "/etc"; allow-query { any; }; // This is the default recursion no; listen-on { 192.168.0.225;...
8
by: aabdis | last post by:
Hello All.... I have a question... i have two Windows apps (one UI, and one service) which use a common DLL that hands out database results and such to the two apps. The DLL always connects to...
2
by: Tonino Greco | last post by:
Hi all, I wonder if someone on the list can offer a suggestion as to how I can parse a config file that has the form: group1 name1 name2 group2
1
by: Bob Bedford | last post by:
Hi all, I need help for parsing a huge XML file with some ISP limits (I can't change ISP, so this isn't a solution). The ISP limits the scripts to 16MB and 10 seconds. Those params can't be...
3
by: deerhide | last post by:
Hi, I was updating my website, well trying to... and I somehow messed it up. I didnt build it, I bought it so I don't know alot about programming. I receive these errors when going to...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.