使用PHP替换PHP脚本文件中的关键字
我有一个混合了html、文本和PHP的PHP文件,其中包含名称areaname-house.PHP。文件的文本/html部分在不同的位置包含字符串“areaname”。另一方面,我有一个带有城市名称的字符串数组 我需要一个PHP脚本,它可以获取每个字符串(来自字符串数组),复制areaname-house.PHP并创建一个名为arrayitem-house.PHP的新文件,然后在新创建的文件中,用arrayitem替换字符串“areaname”。我已经能够完成第一部分,在该部分中,我可以使用示例变量(城市名称)成功创建一个克隆文件,作为以下代码中的测试:使用PHP替换PHP脚本文件中的关键字,php,file,replace,clone,keyword,Php,File,Replace,Clone,Keyword,我有一个混合了html、文本和PHP的PHP文件,其中包含名称areaname-house.PHP。文件的文本/html部分在不同的位置包含字符串“areaname”。另一方面,我有一个带有城市名称的字符串数组 我需要一个PHP脚本,它可以获取每个字符串(来自字符串数组),复制areaname-house.PHP并创建一个名为arrayitem-house.PHP的新文件,然后在新创建的文件中,用arrayitem替换字符串“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
}
?>
更容易的是
$content = file_get_contents($file); //read areaname-house.php
$content = str_replace('areaname', $cityname, $content);
file_put_contents($newfile, $content); //save acton-house.php
因此,不需要显式复制文件
更容易的是
$content = file_get_contents($file); //read areaname-house.php
$content = str_replace('areaname', $cityname, $content);
file_put_contents($newfile, $content); //save acton-house.php
因此,不需要显式复制文件