0

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

1
  • Yes it looks like you are missing how str_replace() works and read about its return value. Commented Nov 12, 2012 at 17:54

3 Answers 3

2

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
));
Sign up to request clarification or add additional context in comments.

1 Comment

the legibility is important +1
1

You need to keep passing the output of each replace to the next replace's input, otherwise you're only replacing in the final str_replace (assuming you're using only the last variable for the rest of your script). Even better you merge the replaces in to less commands -

$template = file_get_contents('/var/www/wiki/' . $wikiname . '/LocalSettings.php');
$new = str_replace(array('templatewiki', 'Templatewiki', 'template_wiki'), $wikiname, $template);
$new = str_replace('/iadmin/wikifresh', '/wiki/' . $wikiname, $new);

You could even merge those two str_replace lines in to one command using array for second param, however for readability I've kept them as two.

You also need to save the output, one way is -

file_put_contents('/var/www/wiki/' . $wikiname . '/LocalSettings.php', $new);

Comments

0

Try:

$template = file_get_contents("/var/www/wiki/". $wikiname ."/LocalSettings.php");
$template = str_replace(array("templatewiki", "Templatewiki","template_wiki","/iadmin/wikifresh"), array($wikiname,$wikiname, $wikiname,"/wiki/".$wikiname), $template);

You need not use multiple str_replace, you need just 1.

You can also use str_ireplace for case-insensitive replace, reducing the parameters by 1,

$template = str_ireplace(array("templatewiki", "template_wiki","/iadmin/wikifresh"), array($wikiname, $wikiname,"/wiki/".$wikiname), $template);

Manuals: str_replace str_ireplace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.