【问题标题】:How to make my php add text on another line [duplicate]如何让我的php在另一行添加文本[重复]
【发布时间】:2017-02-05 23:56:55
【问题描述】:

在我的 PHP 脚本中创建一个新文件并向其中添加文本。 http://localhost/chatpro/chat.php?w=1text

<?php
$msg = $_GET['w'];
$logfile= 'chatroom.chat';
$fp = fopen($logfile, "a");
fwrite($fp, $msg);
fclose($fp);
?>

这会创建一个名为 chatroom.chat 的新文件 在这个文件中,我得到了文本

1

但是在添加新文本时http://localhost/chatpro/chat.php?w=2text 我明白了

1text2text

我想要它,所以它不会在 2 旁边添加 2,而是像列表一样放在下面 例如

1text
2text

【问题讨论】:

    标签: php list line add


    【解决方案1】:

    PHP_EOL 是自 PHP 4.3.10 和 PHP 5.0.2 以来 PHP 中的预定义常量。请参阅手册发布:

    使用它可以为您节省跨平台开发的额外编码。

    只需将您的代码更改为以下内容。

    <?php
    $msg = $_GET['w'].PHP_EOL;
    $logfile= 'chatroom.chat';
    $fp = fopen($logfile, "a");
    fwrite($fp, $msg);
    fclose($fp);
    ?>
    

    【讨论】:

    • 很好的解决方案!!对我来说效果很好
    【解决方案2】:

    您需要在每一行附加一个输入字符(“\n”)。有人喜欢:

    $msg = $_GET['w']."\n";
    

    【讨论】:

      【解决方案3】:

      您可以将字符串“\r\n”添加到行尾。使用双引号,否则将不起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-20
        • 2020-12-21
        • 2016-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多