[英]PHP Regex many matches
我有一個Drupal 配置文件,其中包含如下幾行內容:
* 'driver' => 'mysql',
* 'database' => 'databasename',
* 'username' => 'username',
* 'password' => 'password',
* 'host' => 'localhost',
* 'prefix' => '',
(實際上是2次),它也有一組類似這樣的行:
array (
'database' => 'dba',
'username' => 'admin',
'password' => 'admin1234',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
(僅在文件中出現1次)
如圖所示,區別是我需要定位/匹配的行上沒有星號*(“注釋”標簽)。
我正在使用以下正則表達式,但未返回所需的字符串。
preg_match("#'password' => '(.*?)'#isU",$file_source , $pass);
這是我嘗試的正則表達式模式演示: https : //regex101.com/r/lDA4y4/2
我想要的是密碼值: admin1234
如果只希望沒有*的行,只需檢查以空格開頭的行:
preg_match("/^\s+'password'\s=>\s'(.+)',/im", $source, $matches);
$pass = $matches[1];
因此,基本上,您可以使用^\\s+'password'
定義從行^
到字符串'password'
,它只能包含空格字符\\s+
(1個或更多)。
您可以使用正則表達式破解您的配置文件...
( 這是優化模式的演示 : /^ {6}'password' => '\\K[^']*/m
)
但是,我擔心您會完全丟失擁有.php
配置文件的要點。
如果僅在require
該文件的腳本中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
數組結構)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.