在PHP中如何在文件的中间插入一些文本 [英] How to insert some text in middle of file in PHP

查看:408
本文介绍了在PHP中如何在文件的中间插入一些文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在php中插入一些文本中间的文件,
是可以在文件内容的中间插入文本的方法。



不完全
搜索一个关键字,那么我们需要在内容之前或之后插入,如果搜索关键字发现多于一次文件,然后wat会发生

解决方案

使用 fopen fseek ftell fwrite fclose

  //创建文件处理程序
$ file = fopen(filename ,r +);
//寻找到最后
fseek($ file,SEEK_END,0);
//获取并保存该位置
$ filesize = ftell($ file);
//寻找文件长度的一半
fseek($ file,SEEK_SET,$ filesize / 2);
//写入你的数据
fwrite($ file,Data);
//关闭文件处理程序
fclose($ file);


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

解决方案

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

这篇关于在PHP中如何在文件的中间插入一些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆