function setEnvVal($env_path, $key, $val) {
$env_content = @file($env_path);
$env_data = preg_grep('/^#' . $key . '=|^' . $key . '=/', $env_content);
$old_value = $env_data ? preg_replace('/\r|\n/', '', array_shift($env_data)) : '';
$new_data = $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 setEnvVal($env_path, $key, $val) {
$env_content = @file($env_path);
$env_data = preg_grep('/^#' . $key . '=|^' . $key . '=/', $env_content);
$old_value = $env_data ? preg_replace('/\r|\n/', '', array_shift($env_data)) : '';
$new_data = $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 setEnvVal($env_path, $key, $val) {
$env_content = @file($env_path);
$env_data = preg_grep('/^#' . $key . '=|^' . $key . '=/', $env_content);
$old_value = $env_data ? preg_replace('/\r|\n/', '', array_shift($env_data)) : '';
$new_data = $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');