简体   繁体   中英

PHP Regex many matches

I have a Drupal config file that contains sets of lines like this:

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

(2 times actually) and it also has a set of lines like this:

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

(Occurs only 1 time in the file)

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

What I want is the password value: 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).

See: 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 )

However, I worry that you are completely missing the point/beauty of having a .php config file.

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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