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