Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - Configuration File Processing with PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Configuration File Processing with PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2003-12-13

    Table of Contents:
  • Configuration File Processing with PHP
  • A Simple Configuration File
  • A Grouped Configuration File
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Configuration File Processing with PHP - A Grouped Configuration File


    (Page 3 of 4 )

    A type of configuration file that closely resembles the one we just examined is like that used for the Windows transparency layer for Linux machines, WINE.

    ;; MS-DOS drives configuration
    ;;
    ;; Each section has the following format:
    ;; [Drive X]
    ;; "Path"="xxx"       (Unix path for drive root)
    ;; "Type"="xxx"       (supported types are 'floppy', 'hd', 'cdrom'
    ;;                     and 'network')
    ;; "Label"="xxx"      (drive label, at most 11 characters)
    ;; "Serial"="xxx"     (serial number, 8 characters hexadecimal)
    ;; "Filesystem"="xxx" (supported types are 'msdos'/'dos'/'fat',
    ;;                     'win95'/'vfat', 'unix')
    ;;  This is the FS Wine is supposed to emulate on a certain
    ;;  directory structure.
    ;;

    [Drive C]
    "Path" = "/pack/Work/windows/c"
    "Type" = "hd"
    "Label" = "MS-DOS"
    "Filesystem" = "win95"

    [Drive D]
    "Path" = "/mnt/cdrom"
    "Type" = "cdrom"
    "Label" = "CD-Rom"
    "Filesystem" = "win95"
    ; make sure that device is correct and has proper permissions !

    As we examine the sample from the configuration file we notice that it has been organized into related sections. We also see that the semi-colon is being used to denote comment lines and that we'll also want to treat the double quotes as white space.

    PHP code designed to read such a configuration file might look like this:

    <?php
    $config_file = "my_config.conf";
    $comment = ";";
    $group = "NONE";

    $fp = fopen($config_file, "r");

    while (!feof($fp)) {
      $line = trim(fgets($fp));
      if ($line && !ereg("^$comment", $line)) {
        if (ereg("^\[", $line) && ereg("\]$", $line)) {
          $line = trim($line,"[");
          $line = trim($line, "]");
          $group = trim($line);
        } else {
          $pieces = explode("=", $line);
          $pieces[0] = trim($pieces[0]) , "\"");
          $pieces[1] = trim($pieces[1]) , "\"");
          $option = trim($pieces[0]);
          $value = trim($pieces[1]);
          $config_values[$group][$option] = $value;
        }
      }
    }
    fclose($fp);

    if ($config_values['Drive D']['Type'] == "cdrom")
      echo "Drive D is configured as a CD-Rom device<br />";
    else
      echo "Drive D is not configured as a CD-Rom device<br />";

    Here we've expanded the code from the previous scenario to allow blocked configuration values.

    The $config_file variable still holds the name of the configuration file and $comment has been changed to reflect the use of the semi-colon as a comment delimiter.

    After we obtain a line from the configuration file and have determined it to be something worthwhile, we need to evaluate whether it is a grouping element or a directive. Another comparison to check for an opening and closing brackets at the beginning and end accomplishes this.

    The grouping element is stripped of its brackets and any remaining white space. It's stored as $group and is used as the first branch of our multi-dimensional array so that all related directives can are kept together.

    If the line is a directive then it is split, stripped and stored. The information from the configuration file can be used throughout the script by referencing the array by its group and directive.

    More Miscellaneous Articles
    More By bluephoenix


       ·  } else { $pieces = explode("=", $line); $pieces[0]...
       · Even better is to useexplode("=", $line, 2);Where it is about the third...
       · This is a nice article - but why go to all of that effort when PHP has a built-in...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek