Skip to content

Instantly share code, notes, and snippets.

@dehart
Created December 18, 2013 10:43
Show Gist options
  • Save dehart/8020361 to your computer and use it in GitHub Desktop.
Save dehart/8020361 to your computer and use it in GitHub Desktop.
Change config array with regex
<?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