开发者

PHP regular expression to replace values in a config file

开发者 https://www.devze.com 2023-04-08 07:35 出处:网络
I need to use php to find and replace the database in a config file: $cfg[\'database\'] = \'my_database_name\';

I need to use php to find and replace the database in a config file:

$cfg['database'] = 'my_database_name';

Basically, I'm trying to figure out a regular expression that would find everything between

 $cfg['database'] = 

and

;

then replace it with the actual database name. I think I would need to use preg_replace and then use the matches开发者_Go百科 array, but I'm having a hard time trying to make it work.

Can someone give me an idea of how to go about this, or point me to a good resource that would address this specific type of replacement situation?


preg_replace('/(\$cfg\[\s*\'database\'\s*]\s*=\s*\').*(\'\s*;)', '$1' . $dbName . '$2', $text);
0

精彩评论

暂无评论...
验证码 换一张
取 消