PHP Classes

File: LittleIniParser.php

Recommend this page to a friend!
  Classes of Andrey Nikishaev   Little INI Parser   LittleIniParser.php   Download  
File: LittleIniParser.php
Role: Class source
Content type: text/plain
Description: Little Ini Parser Class
Class: Little INI Parser
Parse INI files and store values in arrays
Author: By
Last change: now we can parse information from N config files
Date: 15 years ago
Size: 2,561 bytes
 

Contents

Class file image Download
<?php
/**
 * Little Ini Parser
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @category Little
 * @package Little_Ini_Parser
 * @copyright Copyright (c) 2008-2020 Nikishaev Andrey (http://andreynikishaev.livejournal.com)
 * @license http://framework.zend.com/license/new-bsd New BSD License
 * @version 1.1
 */

class Little_Ini_Parser {
   
        static
$declared;
       
       
/**
         * Loads configuration from the configuration files
         *
         * @param string $pathes - pathes to the configuration files
         * @param string $sep_k - key separator string
         * @param string $sep_v - value separator string
         * @return array - parsed configuration the configuration files
         */
       
       
public function parse($pathes, $sep_k='.', $sep_v=',') {
           
$conf=array();
            foreach(
$pathes as $mp){
               
$conf+=self::getArr($mp, $sep_k, $sep_v);
            }
            return
$conf;
        }
       
       
/**
         * Loads configuration from the configuration file
         * and returns the associative array
         *
         * @param string $path - path to the configuration file
         * @param string $sep_k - key separator string
         * @param string $sep_v - value separator string
         * @return array - parsed configuration
         */
       
       
private function getArr($path, $sep_k='.', $sep_v=',') {
            if(!
self::$declared) {
                function
tr($value) {
                    return
trim($value);
                }
               
self::$declared=true;
            }
           
           
$out=array();
           
$conf=parse_ini_file($path,true);
            foreach(
$conf as $k=>$v) {
                   
$out[$k]=array();
                    foreach(
$v as $key=>$val) {
                       
$keys=explode($sep_k,$key);
                           
$link=&$out[$k];
                        foreach(
$keys as $key_sep) {
                           
$link=&$link[$key_sep];
                        }
                        if(
eregi(',',$val)) {
                           
$values=explode($sep_v,$val);
                           
array_walk($keys,'tr');
                           
$link=$values;
                        } else {
                           
$link=$val;
                        }
                    }
           
            }
           
            return
$out;
       
        }
}