Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将字符串添加到文件的特定行_Php - Fatal编程技术网

Php 如何将字符串添加到文件的特定行

Php 如何将字符串添加到文件的特定行,php,Php,我的代码是: <?php $data = file_get_contents('file.conf'); $rows = explode("\n", $data); $rcount = count($rows); echo $rcount; for ($l=0; $l<$rcount; $l++) { $rowss = $rows[$l]; if ($rowss == "[default]") { file_put_contents($rowss, "\nhi", FI

我的代码是:

<?php 

$data = file_get_contents('file.conf');
$rows = explode("\n", $data);
$rcount = count($rows); 
echo $rcount;
for ($l=0; $l<$rcount; $l++)
{
$rowss = $rows[$l];
if ($rowss == "[default]")
{
    file_put_contents($rowss, "\nhi", FILE_APPEND | LOCK_EX) or die("<br>oops");
}
}


?>
我的filefile.conf包含52行,已成功打印,但无法在该文件上写入

我需要在[default]行的末尾添加一些字符串,如hi

例如,我的文件是:

eastern=America/New_York|'vm-received' Q 'digits/at' IMp
central=America/Chicago|'vm-received' Q 'digits/at' IMp
central24=America/Chicago|'vm-received' q 'digits/at' H N 'hours'
military=Zulu|'vm-received' q 'digits/at' H N 'hours' 'phonetic/z_p'
european=Europe/Copenhagen|'vm-received' a d b 'digits/at' HM



[default]

1234 => 4242,Example Mailbox,root@localhost
;4200 => 9855,Mark Spencer,markster@linux-    support.net,mypager@digium.com,attach=no|serveremail=myaddy@digium.com|tz=central|maxmsg=        10
;4300 => 3456,Ben Rigas,ben@american-computer.net
;4310 => -5432,Sales,sales@marko.net

您已经犯了两个错误:1,第一个参数必须是写入数据的文件的路径,2个文件附件在文件的末尾插入数据,而不是在中间插入数据;因此,您唯一能做的就是完全覆盖该文件

<?php    
$data = file_get_contents("file.conf");
$rows = explode("\n", $data);
$rcount = count($rows); 
echo $rcount;
$arr = array();
for($l = 0; $l < $rcount; $l++){
    $arr[] = $rows[$l];
    if($rows[$l] == "[default]"){
        $arr[] = "hi";
    }
}    
file_put_contents("file.conf", implode("\n", $arr), LOCK_EX) or die("<br/>oops");
?>

另一种方法是,使用file读取数组,使用array_walk遍历行

<?php
$data = file("file.conf"); // read to array
echo count($data) . "\n";
array_walk($data, function(&$row, $key){
    if ($row == '[default']) {
        $row .= 'SPECIAL';
    }
}) 

file_put_contents("file.conf", implode("\n", $data), LOCK_EX) 
    or trigger_error('Could not save file', E_USER_ERROR);

你改变了什么?当你只粘贴代码的时候,你很难注意到。也就是说,这对海报没什么帮助。问题不应该通过这里的代码修复,而是你的问题是X。X不起作用的原因是因为Y。如果你使用方法Z,那么它将按照你的意愿工作。旁注:有一个函数用于文件\u get\u contents+explode:。@SharanyaDutta它很糟糕,可能会阻止其他人发帖,这也可能会混淆op。也就是说,在我看来,这只是简单地把你的答案放在最上面,然后在后面增加质量,这是不应该做的。发布一个高质量的答案,并加以改进。这和写一篇**保留答案**的帖子没什么区别,这太糟糕了。检查一下:非常感谢莎兰娅·杜塔!
<?php
$data = file("file.conf"); // read to array
echo count($data) . "\n";
array_walk($data, function(&$row, $key){
    if ($row == '[default']) {
        $row .= 'SPECIAL';
    }
}) 

file_put_contents("file.conf", implode("\n", $data), LOCK_EX) 
    or trigger_error('Could not save file', E_USER_ERROR);