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));
}