I have a script that is duplicating a file, but after I duplicate that file, I need a script to dynamically change certain string values inside that file.
Through searching around I found that file_get_contents
and str_replace
work best for this, but for some reason the script I have does not work.
This is what I have: (note: $wikiname is the name of the new wiki being created
$template = file_get_contents("/var/www/wiki/". $wikiname ."/LocalSettings.php");
$snReplace = str_replace("templatewiki", $wikiname, $template);
$mnReplace = str_replace("Templatewiki", $wikiname, $template);
$spReplace = str_replace("/iadmin/wikifresh", "/wiki/".$wikiname, $template);
$wgDBname = str_replace("template_wiki", $wikiname, $template);
Im searching for the exact string and trying to replace that string with the wiki name. But for some reason, this doesn't work at all.
Is there an obvious issue I'm missing?
Thanks
str_replace
returns the modified string, the one you pass as the argument remains unchanged. This is what you need to do:
$template = file_get_contents("/var/www/wiki/" . $wikiname . "/LocalSettings.php");
$template = str_replace("templatewiki", $wikiname, $template);
$template = str_replace("Templatewiki", $wikiname, $template);
$template = str_replace("/iadmin/wikifresh", "/wiki/" . $wikiname, $template);
$template = str_replace("template_wiki", $wikiname, $template);
You can also use strtr
function to achieve same result:
$template = strtr(file_get_contents("/var/www/wiki/" . $wikiname . "/LocalSettings.php"), array(
"templatewiki" => $wikiname,
"Templatewiki" => $wikiname,
"/iadmin/wikifresh" => "/wiki/" . $wikiname,
"template_wiki" => $wikiname
));