x1nick Posted July 17, 2010 Share Posted July 17, 2010 I seem to remember this being possible, but can't remember how its done Got a config file with a bunch of variables EG: #Define array $cfg = array(); #Site name $cfg['site_name'] = 'eg.com'; #Project code $cfg['project_cdoe'] = 'EGW001'; $site = 'This is a example'; How can I update these variables using PHP code? I know I could use fwrite to basically write everything in a file but is there a way to update single variables easily Link to comment https://forums.phpfreaks.com/topic/208029-writing-to-php-variables-within-a-file/ Share on other sites More sharing options...
eevan79 Posted July 17, 2010 Share Posted July 17, 2010 function write_value_of($var,$oldval,$newval) { $contents = file_get_contents('myfile.php'); $regex = '~\\'.$var.'\s+=\s+\''.$oldval.'\';~is'; $contents = preg_replace($regex, "$var = '$newval';", $contents); file_put_contents('myfile.php', $contents); } usage: write_value_of('$site',"This is a example",'Text to replace'); Link to comment https://forums.phpfreaks.com/topic/208029-writing-to-php-variables-within-a-file/#findComment-1087556 Share on other sites More sharing options...
x1nick Posted July 18, 2010 Author Share Posted July 18, 2010 Thanks, didnt know if there was some sort of inbuilt function for this. Link to comment https://forums.phpfreaks.com/topic/208029-writing-to-php-variables-within-a-file/#findComment-1087716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.