[英]How can I delete a line and add other new lines using the erased as a pointer in a file with php?
我的問題是,在這種情況下,如何根據其內容指向一行;請刪除該行,並在哪里繼續寫?
輸入文本:
[SIP34]
include => portabilidad
[SIP85]
include => portabilidad
;*
[SIP51]
include => portabilidad
這是我在文件末尾添加以下行的代碼:
$file = fopen('C:\archivos\extensions.conf', 'a+');
while(!feof($file)) {
$line = fgets($file);
if (stristr($line ,';*')) {
fwrite($file, PHP_EOL.'[new]'.PHP_EOL);
fwrite($file, 'include => portabilidad'.PHP_EOL);
fwrite($file, PHP_EOL.';*'.PHP_EOL);
}
}
fclose($file);
用我的代碼輸出文本:
[SIP34]
include => portabilidad
[SIP85]
include => portabilidad
;*
[SIP51]
include => portabilidad
[new]
include => portabilidad
;*
預期輸出:
[SIP34]
include => portabilidad
[SIP85]
include => portabilidad
[new]
include => portabilidad
;*
[SIP51]
include => portabilidad
它被消除為“; *”,並替換為“ [new]”,然后繼續寫入並放置各個標記以能夠繼續添加
輸出為“ r +”:
[SIP34]
include => portabilidad
[SIP85]
include => portabilidad
;*
[new]
include => portabilidad
;*
[SIP51]
include => portabilidad
易於閱讀,替換和編寫:
$new = '[new]'.PHP_EOL.'include => portabilidad'.PHP_EOL.';*';
$file = 'C:\archivos\extensions.conf';
file_put_contents($file, str_replace(';*', $new, file_get_contents($file)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.