0

关于这个有几个线程,但我找不到任何处理在指定(唯一)点之后将文本插入无序文本文件的方法,而不修改文件的任何其余部分(如果我错过了它,我深表歉意)。

就目前而言,我的代码似乎有效:

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);
?>

但这似乎很不优雅,尤其是开始记录文件内容的开关变量。我认为有比我的解决方案更好的方法来做到这一点。有没有?

感谢您的帮助。

4

1 回答 1

0

按新行分解文件。

这将为您提供每行的漂亮数组。

循环并找到插入点并添加要添加的文本。

然后将其内爆并将其写入文件。

于 2013-06-15T03:09:39.617 回答