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 相关文章推荐
Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口
Apr 18 PHP
php_xmlhttp 乱码问题解决方法
Aug 07 PHP
PHP大小写问题:函数名和类名不区分,变量名区分
Jun 17 PHP
PHP实现今天是星期几的几种写法
Sep 26 PHP
PHP生成Gif图片验证码
Oct 27 PHP
php定义数组和使用示例(php数组的定义方法)
Mar 29 PHP
php中instanceof 与 is_a()区别分析
Mar 03 PHP
php实现短信发送代码
Jul 05 PHP
php实现批量修改文件名称的方法
Jul 23 PHP
windows下的WAMP环境搭建图文教程(推荐)
Jul 27 PHP
不常用但很实用的PHP预定义变量分析
Jun 25 PHP
PHP控制循环操作的时间
Apr 01 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
php中spl_autoload详解
2014/10/17 PHP
浅析php适配器模式(Adapter)
2014/11/25 PHP
Thinkphp3.2简单解决多文件上传只上传一张的问题
2017/09/26 PHP
php实现QQ小程序发送模板消息功能
2019/09/18 PHP
推荐11款jQuery开发的复选框和单选框美化插件
2011/08/02 Javascript
这些年、我收集的JQuery代码小结
2012/08/01 Javascript
提交表单时执行func方法实现代码
2013/03/17 Javascript
原生js拖拽(第一课 未兼容)拖拽思路
2013/03/29 Javascript
js sort 二维数组排序的用法小结
2014/01/24 Javascript
JavaScript中的操作符==与===介绍
2014/12/31 Javascript
MVC Ajax Helper或Jquery异步加载部分视图
2015/11/29 Javascript
网页中JS函数自动执行常用三种方法
2016/03/30 Javascript
使用JavaScript实现弹出层效果的简单实例
2016/05/31 Javascript
Vue.js中用webpack合并打包多个组件并实现按需加载
2017/02/17 Javascript
jQueryeasyui 中如何使用datetimebox 取两个日期间相隔的天数
2017/06/13 jQuery
JS点击缩略图整屏居中放大图片效果
2017/07/04 Javascript
详解vue2.0+axios+mock+axios-mock+adapter实现登陆
2018/07/19 Javascript
Node.js 中如何收集和解析命令行参数
2021/01/08 Javascript
[55:45]LGD vs OG 2019国际邀请赛淘汰赛 胜者组 BO3 第三场 8.24
2019/09/10 DOTA
python实现雨滴下落到地面效果
2018/06/21 Python
python 处理微信对账单数据的实例代码
2019/07/19 Python
pycharm创建scrapy项目教程及遇到的坑解析
2019/08/15 Python
Python类及获取对象属性方法解析
2020/06/15 Python
Python实现AES加密,解密的两种方法
2020/10/03 Python
搭建pypi私有仓库实现过程详解
2020/11/25 Python
总经理助理的八要求
2013/11/12 职场文书
面试后的英文感谢信
2014/02/01 职场文书
酒店管理求职信范文
2014/04/06 职场文书
给校长的建议书400字
2014/05/15 职场文书
酒店开业策划方案
2014/06/02 职场文书
法制宣传标语
2014/06/23 职场文书
企业三严三实学习心得体会
2014/10/13 职场文书
七一建党节慰问信
2015/02/14 职场文书
医生辞职信范文
2015/03/02 职场文书
2016三八妇女节慰问信
2015/11/30 职场文书
CSS的calc函数用法小结
2022/06/25 HTML / CSS