How do I format preg_match() to identify single quote or double quote when matching

First let me say I am not at all familliar with preg_ functions - they are Black Magic !

This is also a follow on to a previous post I made Is it possible to include a file using PHP but NOT the other files that it includes but is an attempt to get the results I want using a different approach.

I have a small script that uses a preg_match function to select database credentials from a Wordpress wp-config.php file. This works fine for variables enclosed with single quotes. However depending on Wordpress settings/version the var may be enclosed in double quotes " then I get an error.

My question is can I have one preg_match function that will return the values whether they are enclosed in single OR double quotes. The code below works great with single quoted variables in the wp-config file such as -

define( 'DB_NAME', 'wp_database' );

Script -

<?php
$file_name = 'wp-config.php';
$value = file_get_contents($file_name);

preg_match('/define.*DB_NAME.*\'(.*)\'/', $value, $m);
$dbname = $m[1];

preg_match('/define.*DB_USER.*\'(.*)\'/', $value, $m);
$dbuser = $m[1];

preg_match('/define.*DB_PASSWORD.*\'(.*)\'/', $value, $m);
$dbpass = $m[1];

preg_match('/define.*DB_HOST.*\'(.*)\'/', $value, $m);
$dbhost = $m[1];

preg_match('/define.*DB_CHARSET.*\'(.*)\'/', $value, $m);
$dbcharset = $m[1];

preg_match('/define.*DB_COLLATE.*\'(.*)\'/', $value, $m);
$dbcollate = $m[1];

echo $dbname, $dbuser, $dbpass, $dbhost, $dbcharset, $dbcollate;
?>

Works great with single quotes ’

but if vars are enclosed in double quotes as follows -

define( 'DB_NAME', "wp_info_1984" );

Then my script gives error -
Notice : Undefined offset: 1 in read_wp_config.php on line 6

I really want a preg_match that detects either. Thanks guys.

I really would advise against that, but keep it simple:

dbconfig.php

define('DB_NAME', 'foo');
define('DB_USER', 'bar');
// etc

wp-config.php

// remove original defines, replace with:
require_once('dbconfig.php');

yourfile.php

require_once('dbconfig.php');

Although I also don’t see a problem in duplicating the config. It’s just a few lines, and if you ever want WP to use a different database than your own code all you need to do is change some values; no refactoring needed.

1 Like

Hi, thanks but the purpose here is not to connect to Wordpress, or create my own config but simply to extract the variable names and values from wp-config

What I’m proposing is create a config that both share, without resorting to preg_match voodoo.

So wp-config uses that config and you use that config.

@rpkamp

The problem is the original config also includes multiple includes itself so I end up with a huge file I don’t want. For this reason I don’t want to clone or duplicate the original config. I just want to access some of the vars in the first config, I am not interested in all the other includes. This is purely an exercise to extract certain vars or defines. I am not interested in a config I can use to connect or amending the original config.

However your suggestion gave me a great idea! I have created a temp copy with the subsequent includes (actually requires in this case) commented out -

<?php
$content = file_get_contents("wp-config.php");
$newcontent = str_replace("require_once", "//require_once", "$content");
file_put_contents("temp-config.php", "$newcontent");
?>

In this way I can include the smaller temp config without all the others and without amending the WP config. This means I can access all the vars. This gives the same result as extracting with preg.

Why use a complex solution when there is an easy solution? :man_shrugging:

Oh well, if it works it works I suppose.

I am so grateful for everyone’s help in this - but you are all overthinking it :smile: (same as related post)
Is it possible to include a file using PHP but NOT the other files that it includes
I do appreciate that many posters are not thinking of all the options and there are better ways of doing things and it is great that we can offer other solutions, but my problem was in each case simply what the question asked.

I am not using Wordpress, I am not connecting to a database, I do not want a functioning config file I simply want to extract the vars from the initial wp-config, for reporting and analysis. There is a Wordpress installed but I am not interested in running it, changing the db, switching dbs creating alternative configs. I really just want to do exactly what the questions ask. The ultimate goal is just to extract the vars and their values.

But seriously - thanks to all as always it is by reading your suggestions and ideas that I finally arrived at a solution that meets my needs. :smile: