php中配置文件操作 如config.php文件的读取修改等操作


Posted in PHP onJuly 07, 2012
<?php 
$name="admin";//kkkk 
$bb='234'; 
$db=4561321; 
$kkk="admin"; 
?>

函数定义:
配置文件数据值获取:function getconfig($file, $ini, $type="string")
配置文件数据项更新:function updateconfig($file, $ini, $value,$type="string")
调用方式:
getconfig("./2.php", "bb");// 
updateconfig("./2.php", "kkk", "admin");

<?php //配置文件数据值获取。 
//默认没有第三个参数时,按照字符串读取提取''中或""中的内容 
//如果有第三个参数时为int时按照数字int处理。 
function getconfig($file, $ini, $type="string") 
{ 
if ($type=="int") 
{ 
$str = file_get_contents($file); 
$config = preg_match("/" . $ini . "=(.*);/", $str, $res); 
Return $res[1]; 
} 
else 
{ 
$str = file_get_contents($file); 
$config = preg_match("/" . $ini . "=\"(.*)\";/", $str, $res); 
if($res[1]==null) 
{ 
$config = preg_match("/" . $ini . "='(.*)';/", $str, $res); 
} 
Return $res[1]; 
} 
} 
//配置文件数据项更新 
//默认没有第四个参数时,按照字符串读取提取''中或""中的内容 
//如果有第四个参数时为int时按照数字int处理。 
function updateconfig($file, $ini, $value,$type="string") 
{ 
$str = file_get_contents($file); 
$str2=""; 
if($type=="int") 
{ 
$str2 = preg_replace("/" . $ini . "=(.*);/", $ini . "=" . $value . ";", $str); 
} 
else 
{ 
$str2 = preg_replace("/" . $ini . "=(.*);/", $ini . "=\"" . $value . "\";",$str); 
} 
file_put_contents($file, $str2); 
} 

//echo getconfig("./2.php", "bb", "string"); 
getconfig("./2.php", "bb");// 
updateconfig("./2.php", "kkk", "admin"); 
//echo "<br/>".getconfig("./2.php", "name","string"); 
?>

//完善改进版 
/** 
* 配置文件操作(查询了与修改) 
* 默认没有第三个参数时,按照字符串读取提取''中或""中的内容 
* 如果有第三个参数时为int时按照数字int处理。 
*调用demo 
$name="admin";//kkkk 
$bb='234'; 
$bb=getconfig("./2.php", "bb", "string"); 
updateconfig("./2.php", "name", "admin"); 
*/ 
function get_config($file, $ini, $type="string"){ 
if(!file_exists($file)) return false; 
$str = file_get_contents($file); 
if ($type=="int"){ 
$config = preg_match("/".preg_quote($ini)."=(.*);/", $str, $res); 
return $res[1]; 
} 
else{ 
$config = preg_match("/".preg_quote($ini)."=\"(.*)\";/", $str, $res); 
if($res[1]==null){ 
$config = preg_match("/".preg_quote($ini)."='(.*)';/", $str, $res); 
} 
return $res[1]; 
} 
} 
function update_config($file, $ini, $value,$type="string"){ 
if(!file_exists($file)) return false; 
$str = file_get_contents($file); 
$str2=""; 
if($type=="int"){ 
$str2 = preg_replace("/".preg_quote($ini)."=(.*);/", $ini."=".$value.";",$str); 
} 
else{ 
$str2 = preg_replace("/".preg_quote($ini)."=(.*);/",$ini."=\"".$value."\";",$str); 
} 
file_put_contents($file, $str2); 
}
PHP 相关文章推荐
建立文件交换功能的脚本(一)
Oct 09 PHP
php抓即时股票信息
Oct 09 PHP
发布一个迷你php+AJAX聊天程序[聊天室]提供下载
Jul 21 PHP
FCKeditor添加自定义按钮
Mar 27 PHP
Ajax PHP 边学边练 之三 数据库
Nov 26 PHP
PHP 删除文件与文件夹操作 unlink()与rmdir()这两个函数的使用
Jul 17 PHP
PHP反转字符串函数strrev()函数的用法
Feb 04 PHP
win7下memCache的安装过程(具体操作步骤)
Jun 28 PHP
PHP实现文件下载断点续传详解
Oct 15 PHP
memcache一致性hash的php实现方法
Mar 05 PHP
php自定义分页类完整实例
Dec 25 PHP
Laravel框架实现调用百度翻译API功能示例
May 30 PHP
php中CI操作多个数据库的代码
Jul 05 #PHP
PHP5权威编程阅读学习笔记 附电子书下载
Jul 05 #PHP
PHP中使用unset销毁变量并内存释放问题
Jul 05 #PHP
php Ubb代码编辑器函数代码
Jul 05 #PHP
PHP取整数函数常用的四种方法小结
Jul 05 #PHP
PHP函数学习之PHP函数点评
Jul 05 #PHP
php中根据变量的类型 选择echo或dump
Jul 05 #PHP
You might like
用 PHP5 轻松解析 XML
2006/12/04 PHP
PHP的substr_replace将指定两位置之间的字符替换为*号
2011/05/04 PHP
PHP 修复未正常关闭的HTML标签实现代码(支持嵌套和就近闭合)
2012/06/07 PHP
php文件上传类完整实例
2016/05/14 PHP
PHP 信号管理知识整理汇总
2017/02/19 PHP
php使用curl_init()和curl_multi_init()多线程的速度比较详解
2018/08/15 PHP
JS控件autocomplete 0.11演示及下载 1月5日已更新
2007/01/09 Javascript
权威JavaScript 中的内存泄露模式
2007/08/13 Javascript
JavaScript 作用域链解析
2014/11/13 Javascript
bootstrap table实例详解
2017/01/06 Javascript
JavaScript初学者必看“new”
2017/06/12 Javascript
vue.js全局API之nextTick全面解析
2017/07/07 Javascript
基于jquery实现多级菜单效果
2017/07/25 jQuery
JavaScript中各数制转换全面总结
2017/08/21 Javascript
Vue结合后台导入导出Excel问题详解
2019/02/19 Javascript
JavaScript 处理树数据结构的方法示例
2019/06/16 Javascript
微信小程序 可搜索的地址选择实现详解
2019/08/28 Javascript
eslint+prettier统一代码风格的实现方法
2020/07/22 Javascript
JavaScript 常见的继承方式汇总
2020/09/17 Javascript
OpenLayers加载缩放控件使用方法详解
2020/09/25 Javascript
浅析微信小程序自定义日历组件及flex布局最后一行对齐问题
2020/10/29 Javascript
Python实现将绝对URL替换成相对URL的方法
2015/06/28 Python
python冒泡排序简单实现方法
2015/07/09 Python
Python爬虫文件下载图文教程
2018/12/23 Python
详解python持久化文件读写
2019/04/06 Python
浅析Python 实现一个自动化翻译和替换的工具
2019/04/14 Python
解决IDEA 的 plugins 搜不到任何的插件问题
2020/05/04 Python
python反爬虫方法的优缺点分析
2020/11/25 Python
关于Python错误重试方法总结
2021/01/03 Python
AmazeUI 面板的实现示例
2020/08/17 HTML / CSS
购买一个高级域名:BuyDomains
2018/03/11 全球购物
《学会合作》教学反思
2014/04/12 职场文书
2015元旦标语横幅
2014/12/09 职场文书
导游词之苏州阳澄湖
2019/11/15 职场文书
深度学习tensorflow基础mnist
2021/04/14 Python
简单且有用的Python数据分析和机器学习代码
2021/07/02 Python