How to replace a certain line of content in php

藏色散人
Release: 2023-03-12 10:04:01
Original
2470 people have browsed it

How to replace the content of a certain line in php: 1. Use the file function to read the file; 2. Get the content of the current line and modify the content through "$fp->write("hello");" .

How to replace a certain line of content in php

The operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

How to replace a certain line in php?

php replaces the content of the specified line in the file

The first type

Use the file function to read the file, each line is an array Element

 $arr =  file($file);
 $arr[$line] = "hello";
 file_put_contents($file, implode("", $arr))
Copy after login

The second type

 $fp = new \SplFileObject('./test.php', 'r+');
 //转到第二行, seek方法参数从0开始计数, 经我测试指针指向行尾了, 所以修改的是第三行
 $fp->seek(1);
 //获取当前行内容(第二行)
 $line = $fp->current();
 $fp->write("hello");
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to replace a certain line of content in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template