【问题标题】:Put comma in txt file using file handling function使用文件处理功能将逗号放入txt文件
【发布时间】:2013-05-25 18:07:52
【问题描述】:

这是我的代码

<?php

  $filename =  'names.txt';
  $file = fopen($filename, 'w');
  fwrite($file, implode(", ", $filename));

?>

我的names.txt文件数据是这样的

saad
Alex
Ashmil
Shumail
Fredrik

我希望在除最后一个名称之外的每个名称之后添加一个 qoma。 .但我收到“传递给内爆函数的参数错误”的错误。告诉我现在该怎么办?

预期的output 应该是

saad, Alex, Ashmil, Shumail

【问题讨论】:

    标签: php file-handling


    【解决方案1】:

    只需使用file():

    $file = 'names.txt';
    $array = file($file); // Creates an array of each line
    array_pop($array); // Remove the last value of the array
    $string = implode(', ', $array); // Implode
    file_put_contents($file, $string); // Write to file
    

    【讨论】:

    • 它起作用了,但它就像这样 saad 、 Alex 、 Ashmel 等等 等等
    • @saad 你想保留换行符吗?
    • @saad 看看我的编辑,添加了PHP_EOL
    • @saad 好的,编辑您的问题并显示预期的输出,否则这将是一个猜谜游戏。
    • @HamZa,我不会使用 PHP,而是像 sedtr 这样的 GNU 工具。
    【解决方案2】:

    使用这个:-

    $file = 'names.txt';
    $array = file($file); // Creates an array of each line
    $array = array_slice($array,0,-1); // Pops the last element of an array
    $string = implode(','.PHP_EOL, $array); // Implode
    file_put_contents($file, str_replace(PHP_EOL,"",$string));
    

    输出:-

    saad, Alex, Ashmil, Shumail
    

    【讨论】:

    • 大声笑,你复制了我的答案,然后用str_replace() 替换了换行符?您可以在 implode 部分中删除 PHP_EOL ...
    • 这行不通。就试一试吧。它不是关于复制,而是提供正确的解决方案。 :)
    • 大声笑,我提供的解决方案首先是“正确的”。 OP只是没有显示他到底想要什么。现在很明显,他想获得最后的价值。所以再次编辑,复制我的答案,享受!
    • 大声笑.. OP 早就说过“他不想保留换行符”。我复制了您的代码,因为您的代码部分正确,并且您的代码需要稍作修改。这就是我所做的。顺便说一句,我不需要复制您编辑的代码,因为这不符合 OP 的要求。
    • 请去看看我的编辑历史。我想指出的是您现在添加换行符,然后删除它们。这没有意义,只是不要一开始就添加它们(就像我在一次编辑中所做的那样)。
    【解决方案3】:

    你可以使用这个代码:)

    <?php
    $filename =  'names.txt';
    $file_read = fopen($filename, 'r');
    $content = fread($file_read, filesize($filename));
    $content = trim(preg_replace('/\s\s+/', ' ', $content));
    $pieces = explode(" ", $content);
    $file_write = fopen($filename, 'w');
    fwrite($file_write, implode(", ", $pieces));
    fclose($file_read);
    fclose($file_write);?>
    

    【讨论】:

    • 为什么?我看到我们不能使用“fwrite”同时读写。所以我用自己的方式创建了两个文件处理程序!
    • 因为不是在不需要的地方使用正则表达式编写这么多行代码,我可以用这样的小命令来完成:sed '{:q;N;s/\n/, /g;t q}' in_file
    • @zps215 谁告诉你不能用一个处理程序 o_o 读写?看看“模式”下的here
    • @HamZa ya 我知道,但在这里我们必须先读取文件然后更新文件。所以如果我使用“r+”模式,那么在 fread 文件指针将位于最后一行之后,现在如果我执行 fwrite,那么它将附加到文件中......使用 'w'/'w+' 我们不能先读取!所以它们没有用。
    • @saad 首先,您将“names.txt”修改为原始文件,其中每个单词后面都有新行,因为在执行这么多操作后它会被覆盖
    【解决方案4】:

    这对我有用:)

        <?php
    
            $file = 'names.txt';
            $array = file($file); // Creates an array of each line
            $array = array_slice($array,0,-1); // Pops the last element of an array
            $string = implode(','.PHP_EOL, $array); // Implode
            file_put_contents($file, str_replace("\n","",$string));   
    ?>
    

    并给了我预期的输出..

    感谢@hamza、@Vivek 和其他所有人 ..

    【讨论】:

      【解决方案5】:

      也试试这个。 它工作......

      <?php
      
      $file = 'names.txt';
      $array = file($file); // Creates an array of each line
      $array = array_slice($array,0,-1); // Pops the last element of an array
      $string = implode(',', $array); // Implode
      file_put_contents($file, str_replace("\n","",$string));
      
      
      ?>
      

      【讨论】:

      • @stano 我检查并提出了我自己的答案以及问题的答案。我根据我的要求放了对我真正有用的东西。
      猜你喜欢
      • 2014-07-02
      • 2015-11-26
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多