/**
* @function 動態的設定env檔案中某項配置值
* @param $env_path string env檔案路徑
* @param $key string 配置項
* @param $val string|int 配置值
* @return bool 回傳是否成功修改
* @other 如果env檔案中沒有這$key這一項,會在env檔案末尾追加
*/
function setEnvVal($env_path, $key, $val) {
//獲取資料
$env_content = @file($env_path);
$env_data = https://www.cnblogs.com/phpphp/archive/2023/01/14/preg_grep('/^#' . $key . '=|^' . $key . '=/', $env_content);
$old_value = https://www.cnblogs.com/phpphp/archive/2023/01/14/$env_data ? preg_replace('/\r|\n/', '', array_shift($env_data)) : '';
//寫入資料
$new_data = https://www.cnblogs.com/phpphp/archive/2023/01/14/$key .'=' . $val;
if($old_value) {
$regex = '/^' . preg_quote($old_value, '/') . '/m';
return (bool) @file_put_contents($env_path, preg_replace($regex, $new_data, implode($env_content, '')));
}
return (bool) @file_put_contents($env_path, PHP_EOL . $new_data, FILE_APPEND);
}
//呼叫
$set_env_res = setEnvVal('env_path', 'key', 'val');
/**
* @function 動態的設定env檔案中某項配置值
* @param $env_path string env檔案路徑
* @param $key string 配置項
* @param $val string|int 配置值
* @return bool 回傳是否成功修改
* @other 如果env檔案中沒有這$key這一項,會在env檔案末尾追加
*/
function setEnvVal($env_path, $key, $val) {
//獲取資料
$env_content = @file($env_path);
$env_data = https://www.cnblogs.com/phpphp/archive/2023/01/14/preg_grep('/^#' . $key . '=|^' . $key . '=/', $env_content);
$old_value = https://www.cnblogs.com/phpphp/archive/2023/01/14/$env_data ? preg_replace('/\r|\n/', '', array_shift($env_data)) : '';
//寫入資料
$new_data = https://www.cnblogs.com/phpphp/archive/2023/01/14/$key .'=' . $val;
if($old_value) {
$regex = '/^' . preg_quote($old_value, '/') . '/m';
return (bool) @file_put_contents($env_path, preg_replace($regex, $new_data, implode($env_content, '')));
}
return (bool) @file_put_contents($env_path, PHP_EOL . $new_data, FILE_APPEND);
}
//呼叫
$set_env_res = setEnvVal('env_path', 'key', 'val');
/**
* @function 動態的設定env檔案中某項配置值
* @param $env_path string env檔案路徑
* @param $key string 配置項
* @param $val string|int 配置值
* @return bool 回傳是否成功修改
* @other 如果env檔案中沒有這$key這一項,會在env檔案末尾追加
*/
function setEnvVal($env_path, $key, $val) {
//獲取資料
$env_content = @file($env_path);
$env_data = https://www.cnblogs.com/phpphp/archive/2023/01/14/preg_grep('/^#' . $key . '=|^' . $key . '=/', $env_content);
$old_value = https://www.cnblogs.com/phpphp/archive/2023/01/14/$env_data ? preg_replace('/\r|\n/', '', array_shift($env_data)) : '';
//寫入資料
$new_data = https://www.cnblogs.com/phpphp/archive/2023/01/14/$key .'=' . $val;
if($old_value) {
$regex = '/^' . preg_quote($old_value, '/') . '/m';
return (bool) @file_put_contents($env_path, preg_replace($regex, $new_data, implode($env_content, '')));
}
return (bool) @file_put_contents($env_path, PHP_EOL . $new_data, FILE_APPEND);
}
//呼叫
$set_env_res = setEnvVal('env_path', 'key', 'val');
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/541992.html
標籤:其他
上一篇:StringBuffer類
下一篇:day01-Spring基本介紹
