1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 配置文件操作.如config.php文件的读取修改等操作

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

时间:2022-10-09 20:03:10

相关推荐

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

php教程|PHP源码

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

php教程-PHP源码

自动评分系统源码,vscode推荐拓展,g41 ubuntu,tomcat的窗口,sqlite收缩用时,缓存网页 插件,开源前端框架有哪些,宿舍大爬虫,php 中的$,黑帽seo大神,php卖号网站程序源码,新年祝福网页,dede商城批发模板,页面添加雪花,学生成绩信息管理系统源码,给赞小程序源码下载lzw

php教务系统源码,ubuntu截图软件下载,爬虫程序如何安装,emplode php|,seo流量因素lzw

php微商城完整源码,vscode 工具栏,多终端ubuntu,tomcat系统服务es,sqlite3表放入内存,脊针爬虫插孢子爬虫,开发php的ide,汕头长沙seo优化团队,学校网站 aspx源码,免费政工网页模板下载lzw

对形如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”);

1. [代码][PHP]代码

<?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 "

".getconfig("./2.php", "name","string");?>

2. [代码][PHP]代码

//完善改进版/** * 配置文件操作(查询了与修改) * 默认没有第三个参数时,按照字符串读取提取\中或""中的内容 * 如果有第三个参数时为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);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。