我有一个Drupal 配置文件,其中包含如下所示的一组行:
* 'driver' => 'mysql',
* 'database' => 'databasename',
* 'username' => 'username',
* 'password' => 'password',
* 'host' => 'localhost',
* 'prefix' => '',(实际上是两次),它也有一组这样的线条:
array (
'database' => 'dba',
'username' => 'admin',
'password' => 'admin1234',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);(仅在文件中发生1次)
正如所示,不同的是,在我需要瞄准/匹配的行上没有星号*(注释标记)。
我使用以下正则表达式,但它没有返回所需的字符串。
preg_match("#'password' => '(.*?)'#isU",$file_source , $pass);这是我尝试的regex模式演示:https://regex101.com/r/lDA4y4/2
我想要的是密码值:admin1234
发布于 2017-12-29 20:36:54
你可以用regex攻击你的配置文件..。
(优化模式的这是演示:/^ {6}'password' => '\K[^']*/m)
但是,我担心您完全忽略了拥有.php配置文件的要点/优点。
如果您只是在需要它的脚本中使用include (或require)文件,那么您就可以直接和干净地访问它所包含的所有变量和配置。这意味着在下列情况下:
include('config.php'); // you may need to modify the path for your case
$db=$databases['default']['default']; // I recommend this line to simplify variable access您将能够访问这样的变量:
$db['database']; // = dba
$db['username']; // = admin
$db['password']; // = admin1234
$db['host']; // = localhost
$db['port']; // [empty string]
$db['driver']; // = mysql
$db['prefix']; // [empty string]因此,您还将从内部的其他未注释声明中获益。
$update_free_access = FALSE;
$drupal_hash_salt = 'N5wfuEGIDhz8C8LuelMlQjkosDt2Avr9ygNciIbmAqw';
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);
ini_set('session.gc_maxlifetime', 200000);
ini_set('session.cookie_lifetime', 2000000);
$conf['404_fast_paths_exclude'] = '/\/(?:styles)|(?:system\/files)\//';
$conf['404_fast_paths'] = '/\.(?:txt|png|gif|jpe?g|css|js|ico|swf|flv|cgi|bat|pl|dll|exe|asp)$/i';
$conf['404_fast_html'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server.</p></body></html>';如果您不需要任何其他声明,那么您可以注释掉它们,或者手动创建一个只包含所需声明的新配置文件(另外,您可以简化$databases数组结构)。
发布于 2017-12-27 11:38:09
如果您只想让行不带*,只需检查以空格开头的行:
preg_match("/^\s+'password'\s=>\s'(.+)',/im", $source, $matches);
$pass = $matches[1];因此,基本上在使用^\s+'password'时,您可以定义从行^开始到字符串'password',它只能包含空格字符\s+ (1或更多)。
https://stackoverflow.com/questions/47990994
复制相似问题