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.