php 修改数组文件,用于修改数组配置文件
/** * 修改扩展配置文件 * @param string $file 文件地地址 * @param string $key 要修改的键名 * @param string $value 修改的值 */ function changConfig($file,$key,$value){ $config_file = file_get_contents($file);//获取文件内容 //正则匹配到指定内容并替换 $pattern = "/\'\s*$key\s*'\s*=>\s*\'\s*([\w-]+)\s*\'/"; $replaceStr= "'$key' => '$value'"; $update_file = preg_replace($pattern, $replaceStr, $config_file); $res = file_put_contents($file, $update_file);//保存 return $res != false? true: false; }