Hongmu Notes
Home Language Notes Use PHP regular expressions to modify parameters in config.php.
Language Notes PHP PHP and mysql

Use PHP regular expressions to modify parameters in config.php.

Use PHP regular expressions to modify parameters in config.php.

config.php

<?php

return array(
    // 数据库类型
    'type'            => 'mysql',
    // 服务器地址
    'hostname'        => '127.0.0.1',
    // 数据库名
    'database'        => 'c51',
    // 用户名
    'username'        => 'root',
    // 密码
    'password'        => 'root',
    // 端口
    'hostport'        => '3306',
    // 连接dsn
    'dsn'             => '',
    // 数据库连接参数
    'params'          => array(),
    // 数据库编码默认采用utf8
    'charset'         => 'utf8',
    // 数据库表前缀
    'prefix'          => 'ey_',
    // 数据库调试模式
    'debug'           => false,
    // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
    'deploy'          => 0,
    // 数据库读写是否分离 主从式有效
    'rw_separate'     => false,
    // 读写分离后 主服务器数量
    'master_num'      => 1,
    // 指定从服务器序号
    'slave_no'        => '',
    // 是否严格检查字段是否存在
    'fields_strict'   => true,
    // 数据集返回类型
    'resultset_type'  => 'array',
    // 自动写入时间戳字段
    'auto_timestamp'  => false,
    // 时间字段取出后的默认时间格式
    'datetime_format' => 'Y-m-d H:i:s',
    // 是否需要进行SQL性能分析
    'sql_explain'     => false,
    // Builder类
    'builder'         => '',
    // Query类
    'query'           => '\\think\\db\\Query',
    // 是否需要断线重连
    'break_reconnect' => false,
);

Use the PHP `preg_replace()` function to modify parameters in a configuration file, such as changing a database username or password:

$config_file = 'config.php';
$config_content = file_get_contents($config_file);

// 修改数据库用户名
$config_content = preg_replace("/'username'\s*=>\s*'[^']*'/", "'username' => 'new_username'", $config_content);

// 修改数据库密码
$config_content = preg_replace("/'password'\s*=>\s*'[^']*'/", "'password' => 'new_password'", $config_content);

file_put_contents($config_file, $config_content);

This example code will read the contents from the config.php file. usernamepassword Replace the parameter values with the new username and password. You can modify the matching rules in the regular expression as needed to match different parameter values. Note that you should back up the original configuration file before making any changes, to prevent data loss or irreversible errors.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180
mysqli in PHP

mysqli in PHP Language Notes PHP PHP collection PHP and mysql

The `mysqli_num_rows()` function is exclusively used with `SELECT` query methods, whereas the `mysqli_affected_rows()` function returns the number of rows affected by the previous SQL statement across the entire database; this function is primarily used with `INSERT`, `UPDATE`, and `DELETE` operations.
👁 145

Recommended reading

(Adaptive mobile version) Bilingual (Chinese/English) mechanical equipment website – PBootCMS template; Download source code for a general-purpose HTML5 machinery website – 0527

(Adaptive mobile version) Bilingual (Chinese/English) mechanical equipment website – PBootCMS template; Download source code for a general-purpose HTML5 machinery website – 0527 Practical Collection pbootcms Template

A bilingual (Chinese/English) PbootCMS website template for mechanical equipment and general machinery applications, compatible with both PC and WAP devices. Its sophisticated, international design makes it ideal for foreign trade enterprises specializing in mechanical equipment to showcase their products. The multilingual support facilitates effective brand promotion for mechanical equipment companies targeting overseas markets. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 52
Responsive branded building materials and tile website template – 0137

Responsive branded building materials and tile website template – 0137 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the branded building materials and tile industry. Its sophisticated and elegant design effectively showcases tile products, spatial applications, and brand identity. It helps building materials brands enhance their online presence and attract designers as well as property owners. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Eyou...
👁 62
Photography – Portraits & Landscape Websites Template 0944

Photography – Portraits & Landscape Websites Template 0944 Practical Collection Yiyou template

An EyouCMS template designed for photography, portrait, and landscape photography websites. Its visual art-style design is ideal for showcasing photographic works, photo albums, landscape images, and photography services. It helps photography studios display their portfolio online and attract clients. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 44
(PC+WAP) Bilingual (Chinese/English) Outdoor Products PbootCMS Website Template – Download Universal Website Source Code for the Outdoor Tent Equipment Industry – 0528

(PC+WAP) Bilingual (Chinese/English) Outdoor Products PbootCMS Website Template – Download Universal Website Source Code for the Outdoor Tent Equipment Industry – 0528 Practical Collection pbootcms Template

A PbootCMS website template designed for the outdoor gear and tent equipment industry, supporting both PC and WAP devices. Featuring an international, minimalist design, this template meets the brand showcase requirements of foreign trade enterprises. It enables outdoor equipment companies to effortlessly create corporate websites tailored for the global market and expand their international business operations. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 59
Culinary and Food Service Training School Website Template 0138

Culinary and Food Service Training School Website Template 0138 Practical Collection Yiyou template

An eyouCMS website template designed for cooking schools, catering and snack businesses, as well as training institutions. Its food and culinary education-themed design enables effective display of cooking courses, snack preparation training programs, faculty expertise, and student projects. This template helps catering training organizations attract learners online and enhance their brand awareness. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eyouCMS | eyou...
👁 43
Household Cleaning Service Website Template 0945

Household Cleaning Service Website Template 0945 Practical Collection Yiyou template

This EYO CMS template is ideal for the household cleaning services industry. Its fresh and professional design effectively showcases cleaning services, household maintenance projects, service personnel, and customer reviews. It helps household services companies build a strong online brand image and attract domestic clients. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 61