I have a little problem with the syntaxis for use preg_replace. I have a function that should replace in a .php file the values of many variables (like a config file).
Example:
file.php:
<?php
$var="string value";
?>
Function:
function savedata($varname, $newvalue){
$data = file_get_contents("file.php");
$newdata = str_replace([find $varname="whatever";], $varname."=$newvalue;", $data);
file_put_contents("file.php", $newdata);
}
And if it runs should make the file to this:
<?php
$var="a new string value";
?>
I find
preg_replace('/"([^"]+)"/', $str, $content)
But works for only the quoted value and if i try add $varname.'='... at start, i get various errors.
Thanks for reading!