僕の備忘録(PC、UN*X、ネットワーク関連が中心)なんです。
自分の書いたところは適当(な時とか)に書き換えますので御了承を。
設定を読み込んで、ひとつの連想配列にしてみた。
設定はこんな感じ。$key = $value "\n" | "\r\n" で、
間にはスペースやらタブやら空(白だけが連続している)行やら
がある前提。
"="は一つしかなく、その前後には空白/改行以外が必ずある前提。
行頭を除いた先頭文字が"#"だったらコメント行とする前提。
$ cat option.txt # This is comment # This is comment, too foo = first bar = second baz =third qux = forth
<?php $wsp = '/[ \t\r\n]/'; $com = '/^#.*$/'; $rep = ''; $file = fopen('option.txt', 'r'); for ($line = fgets($file); ! feof($file); $line = fgets($file)) { // 空白をすべて除く $line = preg_replace($wsp, $rep, $line); // # で始まる行をすべて除く $line = preg_replace($com, $rep, $line); if (strlen($line) != 0) { $ar = explode ('=', $line); $as[$ar[0]] = $ar[1]; } } print_r($as); print $as['bar'] . "\n"; ?>
実行結果。
Array ( [foo] => first [bar] => second [baz] => third [qux] => forth ) second
リンクはご自由にどうぞ。でもURLや内容が変った場合はあしからず。