【问题标题】:How to write file as it is?如何按原样写入文件?
【发布时间】:2013-12-24 20:19:39
【问题描述】:

我有一串这样的代码:

<?php
echo "Hello World";
?>

现在,我想把它写到一个 php 文件中就这样。我的意思是我想用换行符编写这段代码。所以代码以上述方式存储。那么,我该怎么做呢?

我曾尝试使用文件追加和fwrite,但他们将代码写在连续的一行中,我想要将其分成3行

  • 在第一行 - &lt;?php
  • 在第二行 - echo "Hello world";
  • 在第三行 - ?&gt;

但我想只使用一行代码来做到这一点,就像下面的那样。

$file = 'people.php';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file, 
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);

【问题讨论】:

  • 我不明白,您期望的输出是什么?会发生什么?
  • 你想完成什么?
  • 也许问题出在编辑器上而不是你的代码上?就像许多人说的那样,您需要\n 才能获得新线路。你用的是什么编辑器?
  • 您是否正在尝试将 PHP 文件复制到另一个文件中? (如果是这样,请使用copy)。 [或者是你想通过网络向用户输出内容吗?]
  • 哈!这很容易!!!!!!

标签: php newline


【解决方案1】:

使用heredoc应该可以正常工作:

file_put_contents('people.php', <<<HEREDOC
<?php
echo "Hello World";
?>
HEREDOC
);

【讨论】:

    【解决方案2】:

    你的字符串应该是这样的

    $str = "<?php
    echo \"Hello World\";
    ?>";
    

    然后将其写入任何fileName.php,php在解析时忽略新行,只有分号很重要。

    编辑 1

    由于您想将字符串编写为代码,在执行上述步骤后,您将在一行中拥有所有代码,更像是压缩代码,这将是不可读的,为了使其对人类友好,您必须添加格式,格式的最小方法是至少添加新的行字符和制表符以进行缩进。

    为此,您必须做两件事,识别需要添加新换行符的字符 (a) 和需要缩进的字符 (b)。现在在写入文件之前做一些预处理为字符类型(a)添加新的行字符,同时维护一个堆栈以添加适当数量的缩进制表符。

    【讨论】:

      【解决方案3】:

      听起来您需要在每行的末尾添加一个换行符。您可以按如下方式使用正则表达式搜索/替换(其中 $newData 已经包含您要附加​​到文件的字符串):

      <?php
      $newData = preg_replace('/$/',"\n", $newData);
      file_put_contents($file, $newData, FILE_APPEND | LOCK_EX);
      ?>
      

      这会找到每一行的结尾(在正则表达式中由 $ 表示)并在该位置添加新行 (\n)。

      【讨论】:

        【解决方案4】:

        尝试以下:

        <?php
        $file = "helloworld.php";
        $content = file_get_contents($file);
        print_r(htmlspecialchars($content));
        ?>
        

        Helloworld.php

        <?php
        echo "Hello World";
        ?>
        

        它会正常工作的。

        【讨论】:

          【解决方案5】:

          你可以:

          <?php
          $yourContent = <<<PHP
          <?php
           echo "Hello world";
           echo "This in an example";
           $foo = 2;
           $bar = 4;
           echo "Foo + Bar = ".($foo+$bar);
          ?>
          PHP;
          file_put_contents("yourFile.php", $yourContent);
          ?>
          

          【讨论】:

            【解决方案6】:

            尝试在每行代码之间使用以下代码将其放在 php 文件中的新行:

             . PHP_EOL .
            

            【讨论】:

              【解决方案7】:

              此代码在我的电脑上运行。你也会喜欢的。

              <?php
              $file = 'people.php';
              // The new person to add to the file
              $person = "<?php
              echo 'Hello World';
              ?>\n";
              // Write the contents to the file, 
              // using the FILE_APPEND flag to append the content to the end of the file
              // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
              file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
              echo $person;
              ?>
              

              【讨论】:

                【解决方案8】:
                <?php
                 $File = "new.txt"; //your text file
                 $handle = fopen($File, 'w');
                 fwrite($handle, '<?php '."\n".' echo "Hello World "; '."\n".'?>');
                 ?>
                

                【讨论】:

                • 如果 此代码更改为
                • @SanjayRathod Bhadra 想说的是,当您写入文件时,您需要 \n 转义字符作为字符串中的换行符。
                • 但是我从编辑器中获取这段代码,每次代码都不同,那么我如何生成换行符?在这里查看web.guru99.com。在这个编辑器中,用户可以添加执行代码。显然每次都不同,所以我如何插入换行符。
                • 此赏金将在 4 小时后到期,您已将此答案标记为正确。我有更好的答案。如果您在线,请在此处 ping,我会放上去。
                • 这不是通用解决方案。
                猜你喜欢
                • 2016-04-14
                • 1970-01-01
                • 1970-01-01
                • 2018-07-17
                • 2016-12-02
                • 1970-01-01
                • 1970-01-01
                • 2013-03-02
                • 2015-05-04
                相关资源
                最近更新 更多