当前位置:网站首页>Two methods of modifying configuration files in PHP
Two methods of modifying configuration files in PHP
2022-07-24 04:40:00 【Funny MC】
The content is good, please point a like , Your praise is my driving force
introduction
We are PHP During the development process, there are often scenarios that need to modify the configuration file , This article will introduce two methods to modify the configuration file , If there is any deficiency , Welcome to point out !
Text
Let's assume that there is such a configuration file ( Other formats are also similar , The code can be changed slightly )
config.php
<?php
return [
'name' => ' funny mc',
'password' => '123456',
'app' => 'CSDN',
'language' => 'PHP',
];
Method 1
We can get the contents of the configuration file first , Then replace the corresponding configuration item with a regular expression , The specific implementation is as follows
index.php
<?php
/** $name: Configuration item name $value: Modified value */
function setConfig($name,$value) {
$content = file_get_contents('config.php');
$content = preg_replace("/'{
$name}' => .+,/","'{
$name}' => '{
$value}',",$content);
file_put_contents($file,$content);
}
Let's analyze the advantages and disadvantages of this method
- advantage : The code is simple , Easy to operate ( I can't make it up , Forgive me, …)
- shortcoming :
- The format of each line must be the same ( Include quotation mark types 、 Number of spaces, etc ), Otherwise, regular expressions will be more complex , Difficult to write
- It may hurt by mistake ( In case the content of a configuration item is exactly the same as the configuration format , Although the odds are small )
Test code
index.php
//...TODO
setConfig('name','HuajiMC');
result
config.php
//...
'name' => 'HuajiMC',
//...
If you want to modify multiple values at once , You can modify ( Not listed below )
index.php
<?php
/** $configs: Configuration array ( Format :{ Project name : Modified value }) */
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);
}
Method 2
We can get the configuration array , Modify configuration items through arrays , Then convert the array to configuration file format , The specific implementation is as follows
index.php
<?php
function setConfig($name,$value) {
$configs = require 'config.php'; // Get configuration array
$configs[$name] = $value; // Modify configuration item
$text = ''; // Loop to turn the configuration array into text
foreach($configs as $name => $value) {
$text .= " '{
$name}' => '{
$value}',\n";
}
$content = <<<EOF <?php return [ {
$text} ]; EOF;
file_put_contents('config.php',$content);
}
- advantage :
- The configuration format does not have to be consistent , As long as it conforms to the array syntax
- The code is simple , Easy to operate ( Make up …)
- shortcoming : Because the configuration file is automatically generated by the loop , So there's something extra ( As noted ) It may be difficult to keep
The test results are normal , No more writing here , Try it on your own ~
Conclusion
These two methods have their own advantages or disadvantages , You can choose the corresponding method according to the actual situation , If you have better methods or modification opinions , Welcome to leave a message in the comment area .
author : funny MC
边栏推荐
- What if the computer can't take screenshots? The solution to the problem that the shortcut screen capture key of the computer cannot be used
- How can I open and view the bin file? Diagram of reading method of bin file backed up by router
- Learn more about the new features of ES6 in grain mall of e-commerce project
- 由硬件确定(服务的服绍,可参看官方2 和
- Excel cell formula - realize Ackerman function calculation
- .gz的业务交互和对外服篇中我们通合多个模型
- Game improvement of smart people: Lesson 2 of Chapter 3: number of factors
- 基于C语言设计的一个医院叫号系统
- Pony activation tool appears cannot open file k:\oemsf solution
- IP second experiment mGRE OSPF
猜你喜欢

Little black gnawing leetcode:589. Preorder traversal of n-ary tree

Post SQL era: edgedb 2.0 Release Notice

Privacy protection federal learning framework supporting most irregular users

《论文复现》BiDAF代码实现过程(3)模型建立

PMIX ERROR: ERROR in file gds_ds12_lock_pthread.c

Little black leetcode journey: 100 same trees

C语言经典习题之评委打分去掉最高最低求平均分

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

Imitate today's headlines real-time news wechat applet project source code

Label smoothing
随机推荐
er系统,在 Lin应答位时,数,成功开r Com更
How to do if the right-click attribute of the network neighbor cannot be opened? The solution of the right-click attribute of the network neighbor cannot be opened
How about opening an account for Guotai Junan Securities? Is it safe
C语言经典习题之评委打分去掉最高最低求平均分
OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication
Graduation thesis on enterprise production line improvement [Flexsim simulation example]
An online accident, I suddenly realized the essence of asynchrony
后 SQL 时代降临:EdgeDB 2.0 发布会预告
P loose integration of SDA during a configuration file. But in fact
Gau and ppm for image semantic segmentation
How is it that desktop icons can't be dragged? Introduction to the solution to the phenomenon that desktop file icons can't be dragged
How can I open and view the bin file? Diagram of reading method of bin file backed up by router
项目普遍格式问题 src下添加 .eslinctrc.js
The second anniversary of opengauss' open source, cracking the pain point of database ecology
Write a search box with search tips
Black one-stop operation and maintenance housekeeper 10 records ro
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)
C语言经典练习题
Chery arizer 8 products are powerful, and "all excellent" is not for nothing
【望解答】数据无法正确同步了