Created
December 18, 2013 10:43
-
-
Save dehart/8020361 to your computer and use it in GitHub Desktop.
Change config array with regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$boolean = true; | |
$array = <<<'EOT' | |
[ | |
'db'=>'mydb', | |
'user'=>'root', | |
'pass'=>'', | |
'host'=>'localhost', | |
'date'=>$boolean ? 'ja' : 'nee', | |
]; | |
EOT; | |
$newVals=['db'=>'testdb']; | |
echo preg_replace_callback("/'([\w ]+)'=>'([\w ]+)',/", | |
function($matches) use($newVals) { | |
$val = isset($newVals[$matches[1]]) ? $newVals[$matches[1]] : $matches[2]; | |
return "'$matches[1]'=>'$val'',"; | |
}, | |
$array); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment