在PHP中打开txt

时间:2015-12-12 06:07:52

标签: php html

您好我正在尝试创建一个类似聊天系统的php文件,我可以在其中输入文本,然后将其与之前的条目相呼应。在我的文本文件中,它会在结束时显示,但在使用

echo file_get_contents("example.txt")

如果在我的txt文件中有另一个选项显示文件,那么文件只会在每行末尾没有中断显示?

4 个答案:

答案 0 :(得分:2)

尝试这个,我认为它可以根据需要提供输出

echo $string= preg_replace("/\n/", "<br>", file_get_contents('sample.txt'));

答案 1 :(得分:1)

代码可能有很多问题。

如果您使用PHP脚本编写文件,请确保使用双引号。这意味着以下正确

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom ---
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://repo.maven.apache.org:443: Unrecognized Windows Sockets error: 10106: create
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://repo.maven.apache.org:443
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://repo.maven.apache.org:443: Unrecognized Windows Sockets error: 10106: create
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://repo.maven.apache.org:443
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://repo.maven.apache.org:443: Unrecognized Windows Sockets error: 10106: create
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://repo.maven.apache.org:443
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://repo.maven.apache.org:443: Unrecognized Windows Sockets error: 10106: create
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://repo.maven.apache.org:443
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://repo.maven.apache.org:443: Unrecognized Windows Sockets error: 10106: create
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://repo.maven.apache.org:443
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://repo.maven.apache.org:443: Unrecognized Windows Sockets error: 10106: create
Dec 12, 2015 11:56:38 AM org.apache.maven.wagon.providers.http.httpclient.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://repo.maven.apache.org:443
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.414 s
[INFO] Finished at: 2015-12-12T11:56:38+05:30
[INFO] Final Memory: 9M/108M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install-file (default-cli) on project standalone-pom: Execution default-cli of goal org.apache.maven
.plugins:maven-install-plugin:2.4:install-file failed: Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved: Failed to collect d
ependencies at org.apache.maven.plugins:maven-install-plugin:jar:2.4 -> org.codehaus.plexus:plexus-utils:jar:3.0.5: Failed to read artifact descriptor for org.codehaus.plexus:plexus
-utils:jar:3.0.5: Could not transfer artifact org.codehaus.plexus:plexus-utils:pom:3.0.5 from/to central (https://repo.maven.apache.org/maven2): Unrecognized Windows Sockets error:
10106: create -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

不是

<?php fwrite($your_file, "Text to write \n in your file"); ?>

单引号将写入文字字符<?php fwrite($your_file, 'Text to write \n in your file'); ?> 而不是新行,因为单引号通常不会处理您为其提供的字符串。

其次,当您使用\n读取文件时,您将获得一个不包含\ n等字符的字符串。

PHP文档说

  

file_get_contents() - 将整个文件读入字符串

因此,要解决此问题,请将字符串包装在file_get_contents("your_file.txt");标记中,如:

<pre>

<?php echo "<pre>" . file_get_contents("your_file.txt") . "</pre>"; 标记输出预先格式化的字符串,这意味着它的确切含义。

希望这有帮助! :)

答案 2 :(得分:0)

PHP Doc说

  

file_get_contents() - 将整个文件读入字符串

e.g。这会将文件的主体转换为字符串,但是(不可见的等价物)\ n不是字符串char

尝试使用foopen功能。

$handle = fopen("/home/rasmus/file.txt", "r");

答案 3 :(得分:-1)

你可以尝试这个:

echo file_get_contents('./people.txt', true);

您可以参考此页:

LINK

REFER THIS PAGE