PHP用换行符读取纯文本?

时间:2013-06-25 20:45:06

标签: php parsing file-get-contents plaintext

我有一个纯文本文件(text.txt):

11. Bonus: In humans, it is composed of 24 vertebrae plus the sacrum and coccyx. For ten points each:
[10] Identify this support structure found in almost all chordates. An excessive curvature of this structure results in scoliosis, and it usually takes a slim "S" shape in an upright human.
ANSWER: spine (or spinal column; accept backbone; accept vertebral column)
[10] The embryos of many chordates contain this precursor to the spinal column, which is essentially a flexible support rod made of collagen. Unlike most other chordates, lancelets retain this structure as adults.
ANSWER: notochord
[10] These members of the subphylum Urochordata possess notochords as larvae, but then promptly lose it and settle down as sessile filter feeders devoid of any vertebrate characteristics.
ANSWER: tunicates (or sea squirts)

12. Bonus: This novel's protagonist is chased by Detective Fix, who thinks he is a bank robber. For ten points each:
[10] Identify this Jules Verne novel in which Phileas Fogg bets that he can circumnavigate the globe in a certain amount of time.
ANSWER: Around the World in Eighty Days (or Le tour de monde en quatre-vingts jours)
[10] In this Verne novel, Professor Lindenbrock finds a coded note in a Snorri Sturulson book, prompting an adventure through the subterranean world before escaping during an eruption of Stromboli.
ANSWER: Journey to the Center of the Earth (or Voyage au centre de la Terre)
[10] This novel is a sequel to both In Search of the Castaways and Twenty Thousand Leagues Under the Sea. In it, Cyrus Smith and other Union soldiers escape from Richmond on a hot-air balloon, but get stuck in the title locale.
ANSWER: The Mysterious Island (or L'Île mystérieuse)

13. Bonus: He owns the spear Gungnir and sacrificed an eye to drink from the Well of Mimir. For ten points each:
[10] Name this owner of the ravens Hugin and Munin who was the chief Norse deity and father of the Aesir.
ANSWER: Odin (accept Wotan or Woden)
[10] With his brothers Vili and Ve, Odin killed this primordial giant to build the world. He was formed in Ginnungagap by the union of fire and ice.
ANSWER: Ymir (accept Augelmir or close pronunciations of that)
[10] This cold and icy realm met with the fiery Muspelheim to create Ymir. According to some sources, the wicked dead are sent to this place instead of Hel.
ANSWER: Niflheimr

在我的PHP脚本中,我试图读取此文件以便稍后解析,但是当我读取文件时,新行不会出现,整个事情看起来就像一条长行。

这是我的PHP:

ini_set('auto_detect_line_endings',true);
$text = file_get_contents("test.txt");
echo $text;

它有所不同,我在mac上使用Textedit来创建纯文本文件。

3 个答案:

答案 0 :(得分:1)

我认为这是因为文本文件中的换行符存储为'\ n',因此HTML无法识别它。 为此,您必须将所有\n替换为<br />,以便正确回显。

示例:

$text=str_replace("\n","<br />",$text);

我希望它有所帮助.. !!

编辑:

您还可以在$ text周围使用<pre>标签按原样打印。

示例:

echo "<pre>".$text."</pre>";

答案 1 :(得分:0)

这是因为默认情况下,PHP输出的HTTP内容类型标头设置为“text / html”。新行仍然存在(尝试查看源代码)

答案 2 :(得分:0)

header('Content-Type: text/plain');添加到脚本的顶部。所以它看起来像:

<?php
header('Content-Type: text/plain'); 
ini_set('auto_detect_line_endings',true);
$text = file_get_contents("test.txt");
echo $text;
?>
Chrome中的

视图源输出:http://pastie.org/8080064