2009-08-15 10 views
0

私は、html、text、phpを混在させたPHPファイルを持っています。名前はareaname-house.phpです。ファイルのtext/html部分には、さまざまな場所に文字列 "areaname"が含まれています。一方、私は都市名を持つ文字列の配列を持っています。PHPを使用してPHPスクリプトファイルのキーワードを置換する

各文字列(文字列配列から)を取ることができるPHPスクリプトが必要です。areaname-house.phpをコピーし、arrayitem-house.phpという名前の新しいファイルを作成し、新しく作成したファイルで、 areaname "を配列項目に追加します。私は成功し、次のコードでの試験とサンプルの変数(都市名)を使ってクローンファイルを作成することができます最初の部分を行うことができた:

<?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
$content = file_get_contents($newfile); 
$content = str_replace('areaname', $cityname, $content); 
file_put_contents($newfile, $content); 

であっても容易には次のようになります...

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

だから、明示的にファイルをコピーする必要はありません。

+0

素晴らしい作品!ありがとう –

関連する問題