我试图创建一个将ini转换为数组的php类,即:
... 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;
}
}
?>
其他函数(以,throwexception开头)我已经测试过并且工作正常但它仍然返回一个空白数组我认为它在填写之后检查该行是否是注释但它没有提出错误消息所以我不能确定
这里只是我的开始代码:
function throwException($message = null,$code = null) {
throw new Exception($message,$code);
}
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}