user2483916
user2483916

Reputation: 43

PHP Reading plain text with newline?

I have a plain text file (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

In my PHP script I am trying to read this file so I can parse it later, but when I read the file, the newlines do not appear and the whole thing looks like one long line.

Here is my PHP:

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

It it makes any difference, I am using Textedit on the mac to create the plain text file.

Upvotes: 0

Views: 3444

Answers (3)

skrtbhtngr
skrtbhtngr

Reputation: 2251

I think it is because the newline in text file is stored as '\n' and so HTML would not recognize it. For this, you have to replace all \n with <br /> so that they are echoed properly.

Example:

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

I hope it helps..!!

EDIT:

You can also use <pre> tags around $text to print it as it is.

Example:

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

Upvotes: 1

anon
anon

Reputation:

Add header('Content-Type: text/plain'); to the top of your script. So it looks like:

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

view-source output in Chrome: http://pastie.org/8080064

Upvotes: 0

faffaffaff
faffaffaff

Reputation: 3549

It's because PHP outputs with a HTTP content-type header set to "text/html" by default. The newlines are still there (try view source)

Upvotes: 0

Related Questions