本來想用PHP 執(zhí)行Linux sed插入文檔 簡(jiǎn)單方便,但是目前php無權(quán)限執(zhí)行shell命令。請(qǐng)問,用php代碼怎么實(shí)現(xiàn)該要求?
2 回答

瀟湘沐
TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
//a.txtaaa bbb//add text hereccc
$need_add_text = '1111111xxxxxx';$text = file_get_contents('a.txt');$text_new = str_replace('//add text here',"//add text here\n".$need_add_text,$text);file_put_contents('a.txt',$text_new);

慕神8447489
TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
$handle = fopen('log.txt', 'r+');$i = -1;$lastLine = '';while(true){ fseek($handle, $i, SEEK_END); $char = fgetc($handle); if($char == "\n"){ fwrite($handle, "new line \n". $lastLine); exit(); }else{ $lastLine .= $char; } $i --; }
編輯:$lastLine 順序應(yīng)該錯(cuò)了,不過很好修改,就不修改源代碼了,還有一些校驗(yàn)也沒做,主要就針對(duì)樓主需求做個(gè)簡(jiǎn)單示例。關(guān)鍵函數(shù)是:fseek
添加回答
舉報(bào)
0/150
提交
取消