我现在又个 aa.txt文本  里面的数据格式如下:123123123
12312321312
123123123
123123123
123123123现在想做的是 在每个后面加个字母a 然后继续保存到该文档内。 高人指点下~!

解决方案 »

  1.   


    $filename = "yourtext.txt";
    $newContents = '';
    if (!file_exists($filename)){
    die("NG");
    }else{
    $fp = fopen($filename,"rb");
    if (!$fp){
    die("NG");
    }else{
    while(! feof($file)){
    $newContents .= fgets($fp).'a'.'\n';
    }
    }
    fclose($fp);
    }
    file_put_contents($filename,$newContents);
      

  2.   

    $ar = file('aa.txt');
    for($i = 0; $i < count($ar); $i ++) {
       $ar[$i] = rtrim($ar[$i]).'a'.PHP_EOL;
    }
    $file = fopen('aa.txt', 'wb');
    if (fwrite($file, implode('', $ar)) echo '写入成功.';
    fclose($file);
      

  3.   

    订正
    while(! feof($fp)){."\n"
      

  4.   


    $con=file_get_contents('aa.txt');
    $str = str_replace(PHP_EOL, 'a'.PHP_EOL, $con);
    $str=$str.'a';
    file_put_contents('aa.txt',$str);