vote up 0 vote down star

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

	}

?>
flag

1 Answer

vote up 6 vote down check
$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.

link|flag
Work brilliantly !!! thanks – Shahid Aug 15 '09 at 10:30

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.