0 php replace clone file keyword
我有一个混合了html,text和php的PHP文件,包括名称areaname-house.php.该文件的text/html部分包含各个地方的字符串"areaname".另一方面,我有一系列带有城市名称的字符串.
我需要一个PHP脚本,它可以获取每个字符串(来自字符串数组),复制areaname-house.php并创建一个名为arrayitem-house.php的新文件,然后在新创建的文件中,将字符串"areaname"替换为arrayitem.我已经能够完成第一部分,我可以使用示例变量(城市名称)成功创建克隆文件,作为以下代码中的测试:
<?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
}
?>
Run Code Online (Sandbox Code Playgroud)
$content = file_get_contents($newfile);
$content = str_replace('areaname', $cityname, $content);
file_put_contents($newfile, $content);
Run Code Online (Sandbox Code Playgroud)
更容易的是......
$content = file_get_contents($file); //read areaname-house.php
$content = str_replace('areaname', $cityname, $content);
file_put_contents($newfile, $content); //save acton-house.php
Run Code Online (Sandbox Code Playgroud)
因此您无需显式复制文件.
| 归档时间: |
|
| 查看次数: |
337 次 |
| 最近记录: |