/** (V7.3 新增)(V7.4 更新) * 获取env 配置 * @param string $env_key 参数key * @param string $default 默认值 * @return string|bool */ function env($env_key, $default = '') { static $env_data = array(); if (empty($env_data)) { $env_file = YZMPHP_PATH . '.env'; if (!is_file($env_file)) return $default; $lines = file($env_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { $line = trim($line); if (empty($line) || strpos($line, '#') === 0) { continue; } if (preg_match('/^([^=]+)=(.*)$/', $line, $matches)) { $key = trim($matches[1]); $value = trim($matches[2]); if ($value === '') { $env_data[$key] = ''; continue; } $pattern = '/^[\s]*([\'"])(.*?)(?<!\\\\)\1(?=\s*#|$)/'; if (preg_match($pattern, $value, $matches)) { $value = $matches[2]; } else { $parts = explode('#', $value, 2); $value = trim($parts[0]); } $normalizedValue = strtolower($value); if ($normalizedValue === 'true') { $value = true; } elseif ($normalizedValue === 'false') { $value = false; } elseif (is_numeric($value)) { $value = $value + 0; } $env_data[$key] = $value; } } } return isset($env_data[$env_key]) ? $env_data[$env_key] : $default; }
env
上一篇:dispatch
有问题可以加入织梦技术QQ群一起交流学习
本站vip会员 请加入 VIP②群 PS:加入时备注用户名或昵称
普通注册会员或访客 请加入技术交流②群
作者:54818模板网 关注:15 时间:2025-09-01 21:05:02
相关网站教程: