Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: Configuration file parser    Marked Cool    (Review this resource)
Author: vardhan
Posted On: 2005-03-12
Category: Home > PHP Functions

Popularity: 1 points out of 10    

Description: Parses a configuration file (or a string) and stores the elements in a PHP array. The configuration file is much like PHP's configuration file (php.ini).

Total Hits: 914     Total Votes: 0     Total Points: 0 (0 reviews)        [ Download ]   

Page Navigation:  [1]


The code for the parsing function (it can also be downloaded above):


<?php
    
    
/*
        ---------------------------------
        Author: vardhan/exacube
        Date: March 10th, 2005
        ---------------------------------
        
        Reads variables set in a file and loads them into an array.
        This is much like PHP's configuration file (php.ini).
    
        The get_vars() function is the function that parses the configuration file,

  and stores the variables in an array. It then returns the array
        It takes 2 arguments: Content, Type
        
        If you want the function to parse form a file, give the file name+path in the

  first argument, and pass the GET_VARS_FILE statement on the second
        If you want the function to parse a string, the first argument would be the second,

  and the second argument is GET_VARS_STRING
        
        Note that if you're parsing a string, the string is stored in a temporary file and

  then parsed. After its done parsing, the temporary file is deleted.
        You can change the temporary file's name by editing the define() for GET_VARS_TMP_FILE
        
        When parsing, the following features are available:
                Comments supported are C++ style comments (//), Shell (#) and .ini type (;)
                Empty lines are ignored. Multiple white spaces are also ignored.
            
        NOTE: the variable name's regular expression is the same as PHP's variables. If the

  variable name is invalid, then it is not added to the array.
    */
    
    // contants definition
    
define("GET_VARS_FILE", true);
    
define("GET_VARS_STRING", false);
    
define("GET_VARS_TMP_FILE", "tmp_vars_file.txt");
    
    function
get_vars($file, $type = GET_VARS_FILE) {
    
        
// if the type is a string, store the string in a file
        
if ($type == GET_VARS_STRING) {
            
$fp = fopen(GET_VARS_TMP_FILE, "w");
            
fwrite($fp, $file, strlen($file));
            
fclose($fp);
            
$file = GET_VARS_TMP_FILE;
        }
        
        
/*
            -> open the file, initiate the $myvars as an array
            -> read 1 line from the file. If the line is too long, just read 4K
            -> if empty spaces are encountered, go to next line
            -> if the variable has a valid name, add it to the array. Ignore whitespaces
        */
        
        
$fp = fopen($file, "r");
        
$myvars = array();
        
        while (!
feof($fp)) {
            
$dat = fgets($fp, 4096);
            
            if (
$dat[0] == ';' || $dat[0] == '#' || substr($dat, 0, 2) == '//' || preg_match('/^\s*$/', $dat))
                continue;
            
            if (
preg_match('/^\s*([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+)\s*=\s*(.*)$/i', $dat, $matches))
                
$myvars[$matches[1]] = $matches[2];
        }
        
fclose($fp);
        
        
// if $file is a temporary file, delete it.
        
if ($file == GET_VARS_TMP_FILE) @unlink($file);

        return
$myvars;
    }

?>


As mentioned in the comments, this function takes 2 parameters: Content/Filename, and Content-Type
If you are parsing a file, you can pass the 2nd argument as GET_VARS_FILE. If its a string, specify it as GET_VARS_STRING. If this argument is empty, it is considered as a file.

The 1st argument is the content. If you are parsing a string, pass the string as the 1st argument.
If its a file, pass the path of the file name.
If the content is a string, a temporary file is created. The content is then put in the file, parsed, and then the file is deleted.
Here is a small example of this function in action:


<?php
    $string
= '
        // C++ style comment
        ; INI style comment
        # Bash style comment
        
        My_Var = This is the value for the variable "My_Var"
        
        My_Other_Var     =  As you can see, white spacing are ignored.
        
        Final_var = Blank lines are also ignored.
    '
;
    
    echo
'<pre>';
    
    
$arr = get_vars($string, GET_VARS_STRING);
    
print_r($arr);
    
    echo
'</pre>';
?>

Output:

Array
(
     [My_Var] => This is the value for the variable "My_Var"
     [My_Other_Var] => As you can see, white spacing are ignored.
     [Final_var] => Blank lines are also ignored.
)



Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.