0

我正在尝试创建一个将 ini 转换为数组的 php 类,即:

例子.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));
}
4

1 回答 1

1

看一眼parse_ini_file

http://uk3.php.net/parse_ini_file

于 2013-07-25T12:46:19.563 回答