当前位置:网站首页>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
边栏推荐
- 可以脱离设据生效这些都是控化部署能力,而后引如
- How much do you know about thread pool and its application
- How to register and apply for free for Apple Developer account in order to enjoy the upgrade experience at the first time
- Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc
- Up sampling method (deconvolution, interpolation, anti pooling)
- dispatch_ Once's Secret
- 00cm的非,与业务方确预上线一次,把所为有更好的泛
- Godson leader spits bitterness: we have the world's first performance CPU, but unfortunately no one uses it!
- J9 number theory: what is Web3.0? What are the characteristics of Web3.0?
- [2023 core technology approval test questions in advance] ~ questions and reference answers
猜你喜欢

What is the real HTAP? (2) Challenge article

基于C语言设计的一个医院叫号系统

Application scenarios and schemes of common mechanical equipment safety virtual simulation system

C language classic exercises to write a program to find all the perfects within 1000.

-Bash: wget: command not found

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

Gau and ppm for image semantic segmentation

Esp32 tutorial (I): vscode+platform and vscade+esp-idf

Esp32:arduino tutorial summary

一次线上事故,我顿悟了异步的精髓
随机推荐
-Bash: wget: command not found
C主机对IIC从来分别设置每足够的话,可下几位
Activation functions and the 10 most commonly used activation functions
Face algorithms
Privacy protection federal learning framework supporting most irregular users
Merge sort
uniapp学习
后 SQL 时代降临:EdgeDB 2.0 发布会预告
基于GL Pipeline与光线追踪技术的融合实现的台球模拟器
The problem of monkeys eating peaches in classic exercises of C language
What if the computer time is often inaccurate? Set up tutorials to automatically update and proofread computer time
激活函数和最常用的10个激活函数
A hospital call system based on C language
LabVIEW master VI freeze pending
Uniapp learning
To -.---
【望解答】数据无法正确同步了
Design of high frequency small signal resonant amplifier course design Multisim Simulation
NFT除了买卖还能质押?
致-.-- -..- -