我正在尝试用正则表达式替换 wp-config 中的一些内容,但我无法让它工作。
这是我的正则表达式模式:
(?=DB_NAME', ')(.*?)(?<='\);)
这是配置的片段:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
这个想法是替换database_name_here。
和PHP:
$configContent = preg_replace("/(?=DB_NAME', ')(.*?)(?<='\);)/", $databaseName, $configContent);
结果是这样的:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('database-name
/** MySQL database username */
define('DB_USER', 'username_here');
所以模式不起作用。但我不知道如何解决它。为什么环视包含在组中?
我不使用 str_replace 并替换 database_name_here 的原因是因为我将在多种语言中使用它。并且 database_name_here 被翻译成其他语言。