简体   繁体   English

PHP Regex许多匹配项

[英]PHP Regex many matches

I have a Drupal config file that contains sets of lines like this: 我有一个Drupal 配置文件,其中包含如下几行内容:

 *     'driver' => 'mysql',
 *     'database' => 'databasename',
 *     'username' => 'username',
 *     'password' => 'password',
 *     'host' => 'localhost',
 *     'prefix' => '',

(2 times actually) and it also has a set of lines like this: (实际上是2次),它也有一组类似这样的行:

    array (
      'database' => 'dba',
      'username' => 'admin',
      'password' => 'admin1234',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

(Occurs only 1 time in the file) (仅在文件中出现1次)

The difference, as shown, is that there is no star mark * (Comments tags) on the lines that I need to target/match. 如图所示,区别是我需要定位/匹配的行上没有星号*(“注释”标签)。

I'm using the following regex, but it is not returning the desired string. 我正在使用以下正则表达式,但未返回所需的字符串。

preg_match("#'password' => '(.*?)'#isU",$file_source , $pass);

This is my attempted regex pattern demo: https://regex101.com/r/lDA4y4/2 这是我尝试的正则表达式模式演示: https : //regex101.com/r/lDA4y4/2

What I want is the password value: admin1234 我想要的是密码值: admin1234

If you just want the line without the *, just check for the line starting with whitespace: 如果只希望没有*的行,只需检查以空格开头的行:

preg_match("/^\s+'password'\s=>\s'(.+)',/im", $source, $matches);
$pass = $matches[1];

So basically with ^\\s+'password' you define that from the start of the line ^ until the string 'password' , it can only contain whitespace characters \\s+ (1 or more). 因此,基本上,您可以使用^\\s+'password'定义从行^到字符串'password' ,它只能包含空格字符\\s+ (1个或更多)。

See: https://regex101.com/r/KG4inA/2 参见: https : //regex101.com/r/KG4inA/2

You can hack at your config file with regex... 可以使用正则表达式破解您的配置文件...

( here is the demo for the optimized pattern: /^ {6}'password' => '\\K[^']*/m ) 这是优化模式的演示/^ {6}'password' => '\\K[^']*/m

However, I worry that you are completely missing the point/beauty of having a .php config file. 但是,我担心您会完全丢失拥有.php配置文件的要点。

If you just include (or require ) the file in scripts that need it, then you will have direct and clean access to all of the variables and configurations that it holds. 如果仅在require该文件的脚本中include (或require )该文件,那么您将拥有对它所保存的所有变量和配置的直接访问权限。 This means after: 这意味着:

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

You will be able to access variables like this: 您将能够访问像这样的变量:

$db['database']; // = dba
$db['username']; // = admin
$db['password']; // = admin1234
$db['host']; // = localhost
$db['port']; // [empty string]
$db['driver']; // = mysql
$db['prefix']; // [empty string]

As a consequence, you will also benefit from the other non-commented declarations within. 因此,您还将受益于其中的其他未注释的声明。

$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>';

If you don't want any of these other declarations, then you comment them out or manually create a new config file that contains only your desired declarations (additionally you could simplify the $databases array structure). 如果您不希望使用其他任何声明,则可以将其注释掉或手动创建一个仅包含所需声明的新配置文件(此外,您可以简化$databases数组结构)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM