im试图创建一个php类,将ini转换为数组ie:
example.ini…
[helloworld]
testing=1234
数组应该看起来像:
array {
"helloworld" = array {
"testing" = "1234"
}
}
到目前为止,我的代码是:
<?php
require_once "UseFullFunctions.inc.php";
class INI {
protected $Keys = array();
protected $Values = array();
public function __construct($FileName) {
if (!file_exists($FileName)){
throwException('File not found',$FileName);
}
$File = fopen($FileName, 'r');
$isIn = "";
while (($line = fgets($File)) !== false) {
if(!startswith($line,'#')){ // checks if the line is a comment
if(startswith($line,'[')){
$isIn = trim($line,'[]');
$this->Keys[$isIn] = '';
$this->Values[$isIn] = array();
} else {
if ($isIn != ""){
$vars = explode("=",$line);
$this->Values[$isIn][$vars[0]] = $vars[1];
}
}
}
}
var_dump($this->Values);
if (!feof($File)) {
echo "Error: unexpected fgets() fail'n";
}
fclose($File);
}
public function getValues() {
return $this->Values;
}
}
?>
其他函数(以throughexception开头)已经测试过了,工作正常,但它仍然返回一个空白数组。我认为它在检查行是否为注释后就填充了,但它没有出现错误消息,所以我不能确定
只是以防万一,这里是我的开始代码:
function throwException($message = null,$code = null) {
throw new Exception($message,$code);
}
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
看看parse_ini_file
http://uk3.php.net/parse_ini_file