如何打开和写入文件的指定部分


How To fopen and write to a specified section on a file

我似乎不知道如何让这个脚本将数据写入文件的部分。以下是适用代码的摘录:

$file = $url.'.php';   
if (!$file_handle = fopen($file,"a+")) 
{ 
echo $lang['cannot_open_file']; 
} 
if (!fwrite($file_handle,stripslashes(html_entity_decode($data))))
{ 
echo $lang['file_is_not_writable']; 
}
fclose($file_handle);  
echo ($lang['success']);
echo ('<meta http-equiv="Refresh" content="4;url='.$url.'.php" />');
}
}
?>
<form action="<?php echo $url;?>.php" method="post">
<?php echo $lang['name']?> <span style="font-weight:100;">(<?php echo $lang['required']?>)</span>:<br />
<input class="textfield" type="text" name="name" value="" /><br />
<?php echo $lang['website']?> <span style="font-weight:100;">(<?php echo $lang['without_http'];?>)</span>:<br />
<input class="textfield" type="text" name="site" value="" /><br />
<?php echo $lang['message']?>:<br />
<textarea class="textarea" rows="2" cols="25" name="message"></textarea><br />
<img src="captcha-image.php" alt="Image verification" />  <br />
  <?php echo $lang['value_from_the_image'];?>:<br />
<input class="textfield" type="text" name="img_ver" value="" /><br />
        <input type="reset" name="reset" value="<?php echo $lang['reset'];?>" />
        <input type="submit" name="send" value="<?php echo $lang['send'];?>" />
      </form>
<hr>
<h3>Comments:</h3>
<!--comments -->
<?php include('../templates/footer.php'); ?>

有人介意帮我解决这个问题吗?非常感谢。

  1. 使用文件获取内容首先检索内容
  2. 然后使用preg_replace查找要替换的位置或内容
  3. 然后用新内容重写文件