关于这个有几个线程,但我找不到任何处理在指定(唯一)点之后将文本插入无序文本文件的方法,而不修改文件的任何其余部分(如果我错过了它,我深表歉意)。
就目前而言,我的代码似乎有效:
if($file=fopen('dadada.txt','r+'))
{
echo 'File opened';
$switch=0; //This variable will indicate whether the string has has been found.
$back=0; //This counts the number of characters to move back after reaching eof.
}
while(!feof($file))
{
$string=fgets($file);
if($switch==1)
{
$back+=strlen($string);
$modstring=$modstring.$string;
}
if(strpos($string,'thing to find')!==FALSE)
{
echo 'String found';
$switch=1;
$string=fgets($file);
$modstring='test'.$string;
$back+=strlen($string);
}
}
$back*=-1;
fseek($file,$back,SEEK_END);
fwrite($file,$modstring);
fclose($file);
?>
但这似乎很不优雅,尤其是开始记录文件内容的开关变量。我认为有比我的解决方案更好的方法来做到这一点。有没有?
感谢您的帮助。