【问题标题】:unknown symbols when displaying textfile with php用php显示文本文件时的未知符号
【发布时间】:2019-08-26 08:41:13
【问题描述】:

我有一个如下所示的 .txt 文件:

SamAccountName         : spiderman
Name                   : Parker, Peter
LastLogonDate          : 8/22/2019 6:08:17 AM
LastBadPasswordAttempt : 8/22/2019 11:30:58 AM
AccountExpires         : 0

SamAccountName         : ironman
Name                   : Stark, Tony
LastLogonDate          : 8/17/2019 8:39:51 AM
LastBadPasswordAttempt : 5/30/2019 4:59:39 AM
AccountExpires         : 0

SamAccountName         : captainamerica
Name                   : Rogers, Steve
LastLogonDate          : 8/19/2019 2:49:05 AM
LastBadPasswordAttempt : 6/24/2019 1:30:17 AM
AccountExpires         : 0

SamAccountName         : thor
Name                   : Odinson, Thor
LastLogonDate          : 7/25/2019 1:36:11 AM
LastBadPasswordAttempt : 7/25/2019 1:30:35 AM
AccountExpires         : 0

我现在想在我的网站上显示它,因为它使用的是 PHP。我设法做到了,但不知何故总是有这两个未知符号,我不知道如何表达它们的意思,为什么它们在那里以及如何删除它们。 这是我的代码:

<!DOCTYPE html>
<html>

<body>
    <form action="" method="post" class="formbody">
    <div class="codetable">
    <?php
    $file = 'c:\marvel\display.txt';
    $f = fopen($file, "r") or exit("Unable to open file!");
    while(!feof($f))
    {
        echo fgets($f)."<br />";
    }
    fclose($f);
    ?>
    <div class="codelabel"> 
    </div>
    </div>
    </form>
</body>
</html>

这是我得到的未知符号:

或者还有其他(更好的)显示方式吗?

【问题讨论】:

  • 编辑 .txt 文件并删除特殊字符。
  • @suspectus 我没有看到任何特殊字符。 (.txt的内容与问题中的方框相同)
  • 你确定你的脚本在 php 文件中,编码为 UTF-8 w/o BOM,而不是 ANSI 吗?
  • @MorganFreeFarm 在这种情况下,它可能是包含 BOM 的文本文件,而不是脚本本身。
  • 可能duplicate

标签: php html fopen


【解决方案1】:

您的 .txt 文件似乎以错误的格式编码。尝试手动创建一个新的 .txt 文件。您需要使用 ANSI 编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多