php替換檔案指定行的內容

2022-09-16 16:36:12 字數 427 閱讀 8938

1

//第一種 利用file 函式 讀取檔案,每一行都是乙個陣列元素

2$arr = file($file);3

$arr[$line] = "hello";

4file_put_contents($file, implode("", $arr))5

6//第二種7

8 =$fp = new \splfileobject('./test.php', 'r+');9//

轉到第二行, seek方法引數從0開始計數, 經我測試指標指向行尾了, 所以修改的是第三行

10 =$fp->seek(1);

11//

獲取當前行內容(第二行)

12$line = $fp->current

();13

$fp->write("hello");

C 替換檔案中指定的內容

實現替換檔案中指定的內容 created by cryking 2012.02.12 include include include includeusing namespace std char strstr rep char source,char old,char ne 字元替換 strstr...

python替換檔案中的指定內容

編寫的python程式,檔名是file replace.py,具體 如下 usr bin env python coding utf 8 import sys,os if len sys.argv 4 or len sys.argv 5 sys.exit there needs four or fi...

替換檔案內容

前幾天無意間看見一道題,內容大致是這樣的。有乙個檔案,裡面內容是這樣的格式 1 aa 2 bb 3 cc 4 dd 現在想插入3 ee,如果存在3 那麼把後面內容換成新內容,如果不存在則新增一行。一開始我是這麼寫的 後來經指點使用字典可以這麼寫 tmp with open 1 r as fd a f...