PHPで配列型の設定ファイルを読み込むプログラム

PHPコード:
// 変数を設定ファイルに書き込む
public static function writeConfig($key, $value) {
$path = app_path() . '/storage/config_startnews24.php’;
$contents = file_get_contents($path);
$contents = str_replace(self::loadConfig($key), $value, $contents);
file_put_contents($path, $contents);
}

// 設定ファイルから変数を読み込む
public static function loadConfig($key) {
$Config = require(app_path() . '/storage/config_startnews24.php’);
return $Congig[$key];
}

説明:
1.str_replace
検索文字列に一致した全ての文字列を置換する

2.file_put_contents
文字列をファイルに書き込む。filenameが存在しない場合ファイルを作成します。
存在する場合はそのファイルを上書きします。

3.file_get_contents
ファイルの内容を全てに読み込む、失敗した場合、falseを返します。

PHP

Posted by arkgame