views:

159

answers:

1

I have a PHP file with a mix of html, text and php includes name areaname-house.php. The text/html parts of the file contain the String "areaname" in various places. On the other hand I have an array of Strings with city names.

I need a PHP script which can take each string (from strings array), copy the areaname-house.php and create a new file named arrayitem-house.php and then in the newly created file, replace the string "areaname" with the arrayitem. I have been able to do the first part where I can successfully create a clone file using a sample variable (city name) as a test in the following code :

    <?php
 $cityname = "acton";
 $newfile = $cityname . "-house.php";
 $file = "areaname-house.php";

 if (!copy($file, $newfile)) {
  echo "failed to copy $file...n";

 }else{

  // open the $newfile and replace the string areaname with $cityname

 }

?>
+6  A: 
$content = file_get_contents($newfile);
$content = str_replace('areaname', $cityname, $content);
file_put_contents($newfile, $content);

Even easier would be ...

$content = file_get_contents($file); //read areaname-house.php
$content = str_replace('areaname', $cityname, $content);
file_put_contents($newfile, $content); //save acton-house.php

So you don't need to copy the file explicitly.

Philippe Gerber
Work brilliantly !!! thanks