2013-01-13 55 views
0

我想从我的php网页上向我的服务器上的模板文件写几行,然后下载它的一个副本。使用地点标记将文本行写入文件

我将使用包含要填充的表单的main.php。它具有changeme1和changeme2的输入字段。

我会用create.php将在发布细节从main.php读写它们出来的template.php其结构如下(但更大和复杂的):

<html> 
<head> 
<title>Test1</title> 
</head> 

<body> 
<p>{changeme1}</p> 
<p>{changeme2}</p> 

</body> 
</html> 

有没有一种方法可以读取这个template.php,将mods改为{changeme1}和{changeme2},然后写回可下载的文件?我正在考虑搜索和替换?

我已经设法通过使用上所以这里发现的其他信息工作头下载的文件: PHP create file for download without saving on server

但这种方法相呼应的HTML的东西,我需要简单的写在大括号是。由于template.php的最终大小回显并不合适,因为这意味着每次模板更改后都会进行大量工作,修改适合php回显的模板(希望您在此了解我)。

在此先感谢。

回答

1

您可以通过使用file_get_contents()然后用str_replace()替换字符串问题加载文件内容到一个变量。

<?php 
$search = array('{changeme1}', '{changeme2}'); // what will be searched for 
$replace = array('foo', 'bar');     // replace it with this 

$file = file_get_contents ('filename.tpl'); 
$new = str_replace ($search, $replace, $file); 

// $new now contains the template file contents with the values replaced, 
// do what you want with it 
echo $new; 

希望这会有所帮助。

+0

这看起来不错,谢谢。任何使用.tpl或者.php会更好的原因(这就是我将要写的)? – Sara44

+0

任何扩展都可以工作我刚刚在例子中使用了'.tpl'。 – vanneto

+0

'.php'按惯例没有意义,因为它不是PHP脚本。但是使用它没有技术障碍。 –

相关问题