Issue
I would like to extract database access information from a wordpress wp-config.php file.
Example (double quotes):
define( "DB_NAME", "mydatabase" );
But it might as well look like this (single quotes)
define( 'DB_NAME', 'mydatabase' );
A mixed version is also possible:
define( 'DB_NAME', "mydatabase" );
So far I have found this on the internet:
DBNAME=`cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
This works when single quotes are used. What I am looking for is a regex that would handle all cases.
Thank you
Solution
Input file
$ cat wp-config.php
define( 'DB_NAME', "mydatabase" );
or
$ cat wp-config.php
define( 'DB_NAME', 'mydatabase" );
With php:
php -r 'require_once("wp-config.php"); echo DB_NAME;'
With GNU grep
:
$ grep -oP "^define.*?DB_NAME.*?[\042\047]\K[\w.]+" wp-config.php
Output
mydatabase
Answered By - Gilles Quénot Answer Checked By - Robin (WPSolving Admin)