个性原创,打造品牌

拒绝模仿抄袭、拒绝模板建站;
多位资深网页设计师同时设计,让您优中选优;
网站独一无二,品牌轻易提升...

了解详情 >

代码规范,国际标准

后台php+mysql动态管理,功能强大,千万级数据;
前台div+css生成静态,代码精简,访问速度快,
符合w3c国际标准,兼容ie/火狐/苹果等任意浏览器...

了解详情 >

细微高效,全程无忧

域名一个,主机一台;
主机BGP多线路,5G存储空间,流量不限;
新闻录入、图片处理、数据备份、安全监测、网站升级...

了解详情 >

移动时代,机不可失

全新视觉、创新操作;
跨平台兼容,微信无缝对接;
不容错过的手机网络商机,不能错过的手机网站时代...

了解详情 >

搜索领先,锁定访客

在主流搜索引擎的搜索结果中,
网站的名称,排在第一页的第一名,
网站其他相关的3-5个关键词,排在第一页...

了解详情 >

营销利器,决胜千里

全国统一呼叫,品牌企业的象标志。
中国电信4008/4009、中国联通4000/4006,号码任选;
通话资费预存...

了解详情 >
以“信”为主的服务理念,“源”为标杆的服务特点351532641点击咨询010-86203368联系电话
CSS问题设计问题程序问题优化问题

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

发布时间:2015-03-25      浏览次数:2786

$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");

. 代码如下:

//配置文件数据值获取。

//默认没有第三个参数时,按照字符串读取提取''中或""中的内容

//如果有第三个参数时为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 "
".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数据库配置文件一般做法分享
下一篇:DEDE5.7首页会员横排登录代码

Copyright©2016  xinycx.com  All Rights Reserved  北京信源创想科技有限公司  备案号:京ICP备17071760号-1   

京公网安备 11010802036302号


地址:北京市海淀区闵庄路门头馨村北二区33号楼3单元102室  电话:010-86203368  代理域名注册服务机构:北京新网数码信息技术有限公司

北京网站建设 北京网站制作 信源创想 品牌网站建设 企业网站建设 网站设计 APP开发 微信开发 前端切图 DIV+CSS html5切图 APP切图