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

2015-01-24信息快讯网

对形如config.php文件的读取,修改等操作的代码,需要的朋友可以参考下

 
<?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); 
} 
Function eregi is deprecated (解决方法)
深入理解PHP中的Session和Cookie
PHP 使用MySQL管理Session的回调函数详解
浅析PHP中Collection 类的设计
PHP操作MongoDB GridFS 存储文件的详解
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
解析thinkphp基本配置 convention.php
解析php中static,const与define的使用区别
解析php中const与define的应用区别
解决File size limit exceeded 错误的方法
探讨php define()函数及defined()函数使用详解
基于php iconv函数的使用详解
探讨file_get_contents与curl效率及稳定性的分析
基于Zend的Config机制的应用分析
PHP中防止直接访问或查看或下载config.php文件的方法
php数据库配置文件一般做法分享
php中CI操作多个数据库的代码
SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
header中Content-Disposition的作用与使用方法
PHP的异常处理类Exception的使用及说明
php中json_decode()和json_encode()的使用方法
PHP通过session id 实现session共享和登录验证的代码
php提示undefined index的几种解决方法
PHP面向对象的进阶学习(抽像类、接口、final、类常量)
PHP file_exists问题杂谈
PHP filter_var() 函数 Filter 函数
PHP中return 和 exit 、break和contiue 区别与用法
MySQL的FIND_IN_SET函数使用方法分享
PHP运行出现Notice : Use of undefined constant 的完美解决方案分享
PHP持久连接mysql_pconnect()函数使用介绍
PHP中创建空文件的代码[file_put_contents vs touch]
Joomla下利用configuration.php存储简单数据
©2014-2024 dbsqp.com