How to get a piece of text from PHP file -


from wp-config.php file need db name, username, password values without including wp-config file , assign them 3 separate variable further use.

define('db_name', 'somedb'); define('db_user', 'someuser'); define('db_password', 'somepass'); 

my script in same folder. no don't want use wordpress functions.

if really don't want include file, mentioned in comments already,
can read file contents array file().

the iterate on each line , apply cleanup, until format can work with:

<?php  $config = file('wp-config.php');  $dblines = [];  foreach($config $line){     if (stristr($line,"define('db_")!==false){         $dblines[] = $line;     } }  array_walk($dblines, 'cleanup');  // apply cleanup() function members of array, each line  function cleanup(&$value){     // replace leading 'define(' , trailing closing bracket     $value = str_replace(         array("define(",");"),'',$value         );     $value = preg_replace('#\s+//(.*)#','',$value); // remove comments }  // @ point have csv structure single quote delimiter // comma+space separator  $dbconfig = [];  foreach ($dblines $dbline){     // read line separate variables , build array     list($key,$value) = (str_getcsv($dbline,', ',"'"));     $dbconfig[$key] = $value; }  print_r($dbconfig); 

this output

array (     [db_name] => putyourdbnamehere     [db_user] => usernamehere     [db_password] => yourpasswordhere     [db_host] => localhost     [db_charset] => utf8     [db_collate] => ) 

if want access single element array, just

print $dbconfig['db_host']; 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -