当前位置:网站首页>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
边栏推荐
- C语言:随机数的生成
- Esp32 tutorial (I): vscode+platform and vscade+esp-idf
- 由硬件确定(服务的服绍,可参看官方2 和
- Basic learning notes of C language
- OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication
- Will your NFT disappear? Dfinity provides the best solution for NFT storage
- C language: bubble sorting
- Privacy protection federal learning framework supporting most irregular users
- -Bash: wget: command not found
- C language: generation of random numbers
猜你喜欢

LabVIEW master VI freeze pending

PMIX ERROR: ERROR in file gds_ ds12_ lock_ pthread.c

Threejs+shader drawing commonly used graphics

PMIX ERROR: ERROR in file gds_ds12_lock_pthread.c

Gau and ppm for image semantic segmentation
![[cornerstone of high concurrency] multithreading, daemon thread, thread safety, thread synchronization, mutual exclusion](/img/24/16cfb44dde056f4b91cdb1de2e9566.png)
[cornerstone of high concurrency] multithreading, daemon thread, thread safety, thread synchronization, mutual exclusion

uniapp学习

Basic syntax of MySQL DDL and DML and DQL

归并排序(Merge sort)

Little black gnawing leetcode:589. Preorder traversal of n-ary tree
随机推荐
dispatch_ Once's Secret
激活函数和最常用的10个激活函数
What are the 10 live demos showing? It's worth watching again whether you've seen it or not
How about opening an account for Guotai Junan Securities? Is it safe
LabVIEW主VI冻结挂起
Baidu wallet helps you repay the inter-bank repayment of your credit card. The handling fee is 0. Newcomers who arrive in real time will be rewarded with 5 yuan
IP second experiment mGRE OSPF
The judges of C language classic exercises score the highest and lowest to get an average score
佳的性能和可靠性发起写入IIC协类型码和的参数是-4
C language: generation of random numbers
一次线上事故,我顿悟了异步的精髓
PMIX ERROR: ERROR in file gds_ds12_lock_pthread.c
Billiard simulator based on the integration of GL pipeline and ray tracing technology
Why can't I hide folders? Solutions to the hidden folders on the computer that can still be seen
To -.---
LabVIEW master VI freeze pending
项目普遍格式问题 src下添加 .eslinctrc.js
Particle Designer:粒子效果制作器,生成plist文件并在工程中正常使用
[dish of learning notes, dog learning C] Dachang written test, is that it?
Engineer competency model and skill requirements