当前位置:网站首页>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
边栏推荐
- Esp32 tutorial (I): vscode+platform and vscade+esp-idf
- LeetCode 46 全排列
- [network counting experiment report] Cisco LAN Simulation and simple network test
- dispatch_ Once's Secret
- What are the 10 live demos showing? It's worth watching again whether you've seen it or not
- 激活函数和最常用的10个激活函数
- Smart contract: release an erc20 token
- 00cm的非,与业务方确预上线一次,把所为有更好的泛
- LabVIEW master VI freeze pending
- How to get the signature file of Baidu Post Bar? Baidu Post Bar signature file setting and use method graphic introduction
猜你喜欢

Merge sort

Write a search box with search tips

Particle Designer: particle effect maker, which generates plist files and can be used normally in projects

What is the real HTAP? (2) Challenge article

The second anniversary of opengauss' open source, cracking the pain point of database ecology

激活函数和最常用的10个激活函数

Introduction and use of pycharm debugging function

Up sampling method (deconvolution, interpolation, anti pooling)

链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)

LabVIEW master VI freeze pending
随机推荐
-Bash: wget: command not found
The software cannot be uninstalled. Please wait for the current program to complete the uninstallation or change the solution
The judges of C language classic exercises score the highest and lowest to get an average score
How can I open and view the bin file? Diagram of reading method of bin file backed up by router
C语言:冒泡排序法
Smart contract: release an erc20 token
What if the notepad file is too large to open? Introduction to the solution to the phenomenon that txt files are too large to be opened
What is the proper resolution of the computer monitor? Introduction to the best resolution of monitors of various sizes and the selection of different wallpapers
PMIX ERROR: ERROR in file gds_ ds12_ lock_ pthread.c
高频小信号谐振放大器设计-课程设计Multisim仿真
What are the 10 live demos showing? It's worth watching again whether you've seen it or not
Smart people's game improvement: Chapter 3 Lesson 3 example: the secret of prime
Qt5.14_MinGW/MSVC下实现VS2019面板自由拖拽组合功能
How about opening an account for Guotai Junan Securities? Is it safe
后 SQL 时代降临:EdgeDB 2.0 发布会预告
C语言:选择排序法
Esp32:arduino tutorial summary
Solution to the prompt of online account opening and transfer in stock speculation that the depository and transfer services are not activated (China Merchants Bank)
LabVIEW主VI冻结挂起
Threejs+shader drawing commonly used graphics