How to insert some text in middle of file in PHP [

2020-02-11 11:19发布

How to insert some text middle of the file in php, is that ay method available to insert text in middle of file content.

not exactly middle of the file., search a keyword, then we need to insert before or after the content,

if the search keyword found more than one time in file, then wat would be happen

标签: php file
4条回答
欢心
2楼-- · 2020-02-11 11:43

fopen, then fseek, then fwrite

查看更多
干净又极端
3楼-- · 2020-02-11 11:44

Using fopen, fseek, ftell, fwrite, and fclose:

// Create the file handler
$file = fopen("filename", "r+");
// Seek to the end
fseek($file, SEEK_END, 0);
// Get and save that position
$filesize = ftell($file);
// Seek to half the length of the file
fseek($file, SEEK_SET, $filesize / 2);
// Write your data
fwrite($file, "Data");
// Close the file handler
fclose($file);
查看更多
别忘想泡老子
4楼-- · 2020-02-11 11:57
查看更多
看我几分像从前
5楼-- · 2020-02-11 12:01
  1. open source file
  2. open new destination file
  3. copy "first part" of source file into destination file
  4. add new content to destination file
  5. copy "last part" of source file into destination file
  6. close both files
  7. delete original file
  8. rename new file
查看更多
登录 后发表回答