有人可以建议我如何最好地将wp-config.php
中的以下内容与regex
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
答案 0 :(得分:1)
非常有用的正则表达式测试器https://regex101.com/r/gmlGpT/1
define\(['"][A-Z_]+['"]\,\s+['"](?<key>[\w\s]+)['"]\);
答案 1 :(得分:0)
$string = "define('AUTH_KEY', 'put your unique phrase here');'";
$string .= 'define("SECURE_AUTH_KEY", "put your unique phrase here");';
$string .= 'define("LOGGED_IN_KEY", \'put your unique phrase here\');';
preg_match_all("/define\s*\(\s*['\"]([^'\"]+)['\"]\s*,\s*['\"]([^'\"]+)['\"]\s*\)/", $string, $matches);
var_export($matches);
嘿,它很难看,看起来像Perl,我有宾至如归的感觉!
那将产生
array (
0 =>
array (
0 => 'define(\'AUTH_KEY\', \'put your unique phrase here\')',
1 => 'define("SECURE_AUTH_KEY", "put your unique phrase here")',
2 => 'define("LOGGED_IN_KEY", \'put your unique phrase here\')',
),
1 =>
array (
0 => 'AUTH_KEY',
1 => 'SECURE_AUTH_KEY',
2 => 'LOGGED_IN_KEY',
),
2 =>
array (
0 => 'put your unique phrase here',
1 => 'put your unique phrase here',
2 => 'put your unique phrase here',
),
)
基本上,这假设在两个字符串中都没有'nor',那些只会用于开始和结束字符串。它会偶然发生在像define("myKey', 'test')
这样奇怪的事情上,但那些可能是不是问题,因为PHP不喜欢编译它们。
顺便说一下,你可以通过简单地包含wp_config.php然后说print LOGGED_IN_KEY
来避免使用正则表达式。