Class for reading INI files

Simple class that supports reading sections, single values, and arrays from INI files. 
from phpbuilder.com varel

<?
// filename: class.inifile.php
// author: victor m varela (victor@netims.com)
// comment: *.ini file reader for PHP
// license: LGPL
// date: mié oct 25 10:20:10 CEST 2000
//
// vie oct 27 12:25:48 CEST 2000
// 1.01 recognize some bool values (true, false, yes, no, on, off)
//
// vie oct 27 16:00:36 CEST 2000
// 1.02 bugfix: false value was returning default value
//
// lun oct 30 11:55:38 CET 2000
// 1.03 cached engine added
//
// dom dic 3 04:04:36 CET 2000
// 1.04 bug with "=" symbol fixed
// David P. Schwartz (davids@desertigloo.com)
//
// valid formats:
//
// [my-section]
// # single values
// var1 = value1
// var2=value 2
// var3 = 'value 3'
// ; if we use values between ' or ", they are not trimmed
// var4=" value 4"
//
// ; setting an array
// var_array1 = this is var_array1[0]
// var_array1 = this is var_array1[1]
// var_array1 = this is var_array1[2]
//
// sample_var = this is a = symbol
//

// cached values
$iniCachedValues = array();
$iniCachedSections = array();
$iniCachedFilenames = array();

class IniFile {

// private: do not use;
var $filename;

// constructor
function IniFile ($filename)
{
global $iniCachedValues, $iniCachedSections, $iniCachedFilenames;
$section = "";
$this->filename = $filename;
if (file_exists($filename) and ! isset($iniCachedFilenames[$filename])) {
$iniCachedFilenames[$filename] = $filename;
$lines = file($filename);
for ($i = 0; $i < count($lines); $i++) {
$lines[$i] = trim($lines[$i]);
if ($lines[$i][0] == ';' || $lines[$i][0] == '#') {
// it's a commment
continue;
} elseif (ereg("^/[(.*)/]", $lines[$i], $regs)) {
// new section
$section = strtolower(trim($regs[1]));
$iniCachedSections[$filename][] = trim($regs[1]);
} elseif (preg_match("'^(.*?)[/s]*=[/s]*(.*)'", $lines[$i], $regs)) {
// setting variable
$variable = strtolower(trim($regs[1]));
$value = trim($regs[2]);
if (ereg("^[/"'](.*)[/"']$", $value, $regs)) {
$value = $regs[1];
}
$lower_value = strtolower($value);
if (in_array($lower_value, array('true', 'yes', 'on'))) {
$value = 1;
}
elseif (in_array($lower_value, array('false', 'no', 'off'))) {
$value = 0;
}
if (! isset($iniCachedValues[$filename][$section][$variable])) {
// new variable
$iniCachedValues[$filename][$section][$variable] = $value;
}
elseif (gettype($iniCachedValues[$filename][$section][$variable]) == "array") {
// append new element to array
$iniCachedValues[$filename][$section][$variable][] = $value;
}
else {
// two values for a single variable, convert to array
$ovalue = $iniCachedValues[$filename][$section][$variable];
$iniCachedValues[$filename][$section][$variable] = array($ovalue,$value);
}
}
}
}
}

// returns file name
function fileName () { return $this->filename; }

// returns all sections
function readSections () {
global $iniCachedSections;
echo serialize($iniCachedSections);
return $iniCachedSections[$this->filename];
}

// returns all (variable,value) pairs for a section
function readSection ($section) {
global $iniCachedValues;
return $iniCachedValues[$this->filename][strtolower($section)];
}

// returns a value
function value($section, $variable, $default)
{
global $iniCachedValues;
$section = strtolower($section);
$variable = strtolower($variable);
if (isset($iniCachedValues[$this->filename][$section][$variable])) {
return $iniCachedValues[$this->filename][$section][$variable];
}
else {
return $default;
}
}
}

?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值