How to parse a config file into an array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HugoFromBOSS
    New Member
    • Sep 2007
    • 38

    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:
    [code=php]
    //comment
    variable = "value" //for text
    variable = value //numeric
    [/code]
    I have managed to get php to output an array like so (using the explode function)
    [code=php]
    Array
    (
    [0] => variable = "value"
    )

    and not ...

    Array
    (
    [ variable] => "value"
    )[/code]

    How do I do this?

    Many Thanks!
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    [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_variab le=>$v_variable );[/PHP]

    Comment

    • HugoFromBOSS
      New Member
      • Sep 2007
      • 38

      #3
      Ah, but the config is in a file called config.cfg

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by HugoFromBOSS
        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

        Comment

        • HugoFromBOSS
          New Member
          • Sep 2007
          • 38

          #5
          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!

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            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

            Comment

            • kovik
              Recognized Expert Top Contributor
              • Jun 2007
              • 1044

              #7
              Take a look at parse_ini_file( ).

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Originally posted by volectricity
                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

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  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

                  Comment

                  • HugoFromBOSS
                    New Member
                    • Sep 2007
                    • 38

                    #10
                    Yeh, I tried "parse_ini_file ()" but it was a bit crasy.

                    Comment

                    • HugoFromBOSS
                      New Member
                      • Sep 2007
                      • 38

                      #11
                      [code=php]
                      <?

                      $url = ('server.cfg');

                      $fh = fopen($url, 'r');
                      $file_cfg = fread($fh,10000 );
                      fclose($fh);

                      list($key,$val) = split(' ', $file_cfg);
                      $array[$key]=$val;

                      print_r($array['servername']

                      ?>
                      [/code]
                      That only gets the first line.

                      How Do I get all of them?
                      Last edited by Atli; Sep 12 '07, 08:11 PM. Reason: Added [code] tags. Please use [code] tags when posting code.

                      Comment

                      • HugoFromBOSS
                        New Member
                        • Sep 2007
                        • 38

                        #12
                        BTW I got rid of the '=' the values are split by an ' '

                        Comment

                        • ronverdonk
                          Recognized Expert Specialist
                          • Jul 2006
                          • 4259

                          #13
                          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

                          Comment

                          • pbmods
                            Recognized Expert Expert
                            • Apr 2007
                            • 5821

                            #14
                            Heya, Hugo.

                            Please use CODE tags when posting source code:

                            &#91;CODE=ph p]
                            PHP code goes here.
                            &#91;/CODE]

                            parse_ini_file( ) looks like it will do exactly what you're looking for. Pay special attention to the process_section s parameter.

                            Comment

                            • HugoFromBOSS
                              New Member
                              • Sep 2007
                              • 38

                              #15
                              I'll take another look

                              Comment

                              Working...