当前位置:网站首页>PHP修改配置文件的两种方法
PHP修改配置文件的两种方法
2022-07-24 04:38:00 【滑稽mc】
内容不错请点个赞吧,您的点赞是我前进的动力
引言
我们在PHP开发过程中经常有需要修改配置文件的场景,本文将介绍两种修改配置文件的方法,如有不足,欢迎指出!
正文
我们假设有个这样的配置文件(如果是其他格式也差不多,可将代码稍加改动)
config.php
<?php
return [
'name' => '滑稽mc',
'password' => '123456',
'app' => 'CSDN',
'language' => 'PHP',
];
方法一
我们可以先获取配置文件内容,然后使用正则表达式替换对应的配置项,具体实现如下
index.php
<?php
/** $name:配置项名称 $value:修改后的值 */
function setConfig($name,$value) {
$content = file_get_contents('config.php');
$content = preg_replace("/'{
$name}' => .+,/","'{
$name}' => '{
$value}',",$content);
file_put_contents($file,$content);
}
我们来分析一下这种方法的优缺点
- 优点:代码简单,操作简便(实在编不出来了,原谅我…)
- 缺点:
- 必须保证每行配置的格式相同(包括引号类型、空格数量等),否则正则表达式会比较复杂,难书写
- 可能会误伤(万一某个配置项的内容刚好和配置的格式相同,虽然几率很小)
测试代码
index.php
//...TODO
setConfig('name','HuajiMC');
结果
config.php
//...
'name' => 'HuajiMC',
//...
如果你想一次性修改多个值,可以这样修改(下文不再列出)
index.php
<?php
/** $configs:配置数组(格式:{项目名称:修改值}) */
function setConfig($configs){
$content = file_get_contents('config.php');
foreach($configs as $name => $value) {
$content = preg_replace("/'{
$name}' => .+,/","'{
$name}' => '{
$value}',",$content);
}
file_put_contents($file,$content);
}
方法二
我们可以将配置数组获取,通过数组修改配置项,然后将数组转换成配置文件格式,具体实现如下
index.php
<?php
function setConfig($name,$value) {
$configs = require 'config.php'; //获取配置数组
$configs[$name] = $value; //修改配置项
$text = ''; //循环将配置数组转为文本
foreach($configs as $name => $value) {
$text .= " '{
$name}' => '{
$value}',\n";
}
$content = <<<EOF <?php return [ {
$text} ]; EOF;
file_put_contents('config.php',$content);
}
- 优点:
- 配置格式不必保持一致,只要符合数组语法即可
- 代码简单,操作简便(凑数…)
- 缺点:因为配置文件由循环自动生成,所以有些额外内容(如注释)可能比较难保留
测试结果正常,这里就不再写了,可以自己尝试一下~
结语
这两种方法各有各的优点或缺点,可以结合实际情况选择相应的方法,如果你有更好的方法或修改意见,欢迎评论区留言提出。
作者:滑稽MC
边栏推荐
- Export function called separately
- [untitled]
- Teacher qiniu said is the VIP account opened for you safe?
- What is the real HTAP? (2) Challenge article
- Post SQL era: edgedb 2.0 Release Notice
- Traversal of binary tree
- 00cm non, make sure to go online with the business party once in advance, and make everything better
- Why can't I hide folders? Solutions to the hidden folders on the computer that can still be seen
- Qt5.14_MinGW/MSVC下实现VS2019面板自由拖拽组合功能
- 链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)
猜你喜欢

LabVIEW主VI冻结挂起

Design of two power dividers and directional couplers for basic experiment of microwave technology

基于GL Pipeline与光线追踪技术的融合实现的台球模拟器

归并排序(Merge sort)

J9 number theory: what is Web3.0? What are the characteristics of Web3.0?

What is the real HTAP? (2) Challenge article

Design of high frequency small signal resonant amplifier course design Multisim Simulation

Nautilus 3.19.2为Gnome增添动力

高频小信号谐振放大器设计-课程设计Multisim仿真

LabVIEW master VI freeze pending
随机推荐
到3mm;提供安全稳定的产品作的执行据发出方iid
Export function called separately
How to play the Microsoft twin tool twinsonot? Introduction to twin test tool twinornot
Journey of little black leetcode: 590. Post order traversal of n-ary tree
Imitate today's headlines real-time news wechat applet project source code
C语言:随机数的生成
PMIX ERROR: ERROR in file gds_ ds12_ lock_ pthread.c
Graduation thesis on enterprise production line improvement [Flexsim simulation example]
Black one-stop operation and maintenance housekeeper 10 records ro
How can I open and view the bin file? Diagram of reading method of bin file backed up by router
Teacher qiniu said is the VIP account opened for you safe?
Which is better, Xiaomi current treasure or yu'e Bao? Detailed comparison and introduction of the differences between Xiaomi current Bao and Alibaba yu'e Bao
[essay] goodbye to Internet Explorer, and the mark of an era will disappear
【望解答】数据无法正确同步了
Uniapp learning
Shell syntax (2)
A hospital call system based on C language
在一线城市上班的程序员,家庭一般是怎样的?
Esp32 tutorial (I): vscode+platform and vscade+esp-idf
LabVIEW master VI freeze pending