使用 PHP 处理文件
任何人都可以告诉使用 PHP 修改/删除文本文件内容的方法吗
Can anyone tell the method to modify/delete the contents of a text file using PHP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
任何人都可以告诉使用 PHP 修改/删除文本文件内容的方法吗
Can anyone tell the method to modify/delete the contents of a text file using PHP
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
使用
file_put_contents
:如果您想追加到文件而不是替换其内容,请使用:(
file_out_contents
是使用整个fopen
的更简单替代方案复杂的。)Using
file_put_contents
:If you want to append to the file instead of replacing it's contents use:
(
file_out_contents
is the simpler alternative to using the wholefopen
complex.)通过使用 fopen:
将“”写入文件以删除其内容。
要逐行删除内容,请使用:
然后写入文件内容。或者您指的是内存映射文件?
By using fopen:
Write "" to a file to delete its contents.
To delete contents line by line use:
then write the file contents. Or are you referring to memory mapped files?
读取文件的方法有很多种。 可以非常快速地处理大文件
使用您还可以使用 file_get_contents() 来读取文件, 。这是实现同样目标的简短方法。
要修改或附加文件,您可以使用
“您还可以将文件加载到数组中,然后执行搜索操作来删除数组的特定元素”。
$lines = file('带有完整路径的文件。')
; // 路径中的单斜杠应该是双斜杠,例如 C://PATH //TO //FILE.TXT上面的代码将加载 $lines 数组中的文件。
There are number of ways to read files. Large files can be handled very fast using
you can also use file_get_contents() to read files. its short way of achieving same thing.
to modify or append file you can use
YOU CAN ALSO LOAD THE FILE INTO ARRAY AND THEN PERFORM SEARCH OPERATION TO DELETE THE SPECIFIC ELEMENTS OF THE ARRAY.
$lines = file('FILE WITH COMPLETE PATH.')
; // SINGLE SLASH SHOULD BE DOUBLE SLASH IN THE PATH SOMTHING LIKE C://PATH//TO//FILE.TXTAbove code will load the file in $lines array.