Location: PHPKode > projects > Parser > parser/Parser.php
<?
class Parser {
        var $error = '';
        var $secs;
        var $prms;

        function Parser($cfg_file) {
                if (file_exists($cfg_file)) {
                        $fp = fopen($cfg_file,'r');
                        $sec = '';
                        while ($buffer = fgets($fp, 4096)) {
                                $buffer = trim($buffer);
                                # Section
                                if (preg_match('/\[([\w\s]*)\]/',$buffer,$ar)) {
                                        $this->secs[] = $ar[1];
                                        $sec = $ar[1];
                                }
                                # Comment
                                if (preg_match('/^#/',$buffer)) {
                                        continue;
                                }
                                # Empty line
                                if (empty($buffer)) {
                                        continue;
                                }
                                # Parameter
				if (preg_match('/(\w*)\s*=\s*(.*?)\Z/',$buffer,$ar)) {
                                        $this->prms[$sec][$ar[1]]=$ar[2];
                                }
                        }
                        fclose($fp);
                        $this->error_text = '';
                }
                else {
                        $this->error = "File doesn't exists";
                }
        }

        function sections() {
                reset($this->secs);
                $ar = array();
                while (list($key,$val)=each($this->secs)) {
                        $ar[]=$val;
                }
                return $ar;
        }

        function parameters($section_name) {
                reset($this->prms);
                $ar = array();
                while (list($key,$val)=each($this->prms[$section_name])) {
                        $ar[]=$key;
                }
                return $ar;
        }

        function param($section_name,$param_name) {
                return $this->prms[$section_name][$param_name];
        }

        function has_parameter($section_name='',$param_name) {
                if (!empty($section_name)) {
                        if (isset($this->prms[$section_name][$param_name])) {
                                return 1;
                        }
                        else {
                                return 0;
                        }
                }
                else {
                        $ss = $this->secs();
                        while (list($key,$sec_name)=each($ss)) {
                                $ss1 = $this->parameters($sec_name);
                                while (list($key,$par_name)=each($ss1)) {
                                        if ( ($par_name == $param_name) && (isset($this->prms[$sec_name][$par_name])) ) {
                                                return 1;
                                        }
                                        else {
                                                return 0;
                                        }
                                }
                        }
                }
        }

        function error() {
                return $this->error;
        }

}
?>
Return current item: Parser