Do you want "normal" text that's formatted in a fixed width font, with white spaces and tabs and new line characters to have a similar appearance on a browser? The default answer of you generate a page of HTML is "tough luck", as text defaults to a variable width font, lines are squished together with multiple spaced becoming single spaces, and even new lines are treated as just white space.
You want:
/dev/disk0s3 117089712 83067248 33766464 72% /
devfs 106 106 0 100% /dev
fdesc 1 1 0 100% /dev
map -hosts 0 0 0 100% /net
map auto_home 0 0 0 100% /home
/dev/disk1 3674142 3674142 0 100% /Vol/MOS
but you get:
/dev/disk0s3 117089712 83067248 33766464 72% / devfs 106 106 0 100% /dev fdesc 1 1 0 100% /dev map -hosts 0 0 0 100% /net map auto_home 0 0 0 100% /home /dev/disk1 3674142 3674142 0 100% /Vol/MOS
How can you achieve the effect you desire? There are or have been a number of options:
a) The
<PRE> to
</PRE> tag pair - though even within there you need to be careful of
& and
< characters.
b)
white-space: pre; within your CSS - but there are issues with how some versions of Internet Explorer handle that attribute
c) Specify
Content-Type: text/plain in your headers, but that will turn the whole document into pure text - rather like letting the tail wag the dog
d) Setting the columns of data you see in the example above into columns of a table to achieve a display which is not identical, but has a similar readability
e) Using a fixed width font such as courier, converting spaces to
special characters, and converting new lines into <br /> tags.
Take your choice - there are times that each option is a good one. If you're coding in PHP, then there's a function in the language -
nl2br - that converts new lines to line breaks, and you could also use
preg_replace to convert spaces into
- s
Here's a sample PHP program that uses
nl2br - and I have also taken the opportunity to illustrate a "here document" which allows me to add a multi line block of text within my PHP. The
<<< syntax is oft overlooked ....
<?php
$line = "The Pie Shop\n25 High Street\nSalisbury\nWilts\n\n";
print ($line);
print (nl2br($line));
$haircut = <<<BALD
I used to have %colour% hair, then I went
grey and then I became hairless. With %colour%
hair, they used to call me "bushy". With grey,
they thought I was a whizz or wizzard, and now
I'm called Coot.
BALD;
$output = preg_replace("/%colour%/","ginger",$haircut);
print (nl2br($output));
When I run this from the command line, I get:
The Pie Shop
25 High Street
Salisbury
Wilts
The Pie Shop<br />
25 High Street<br />
Salisbury<br />
Wilts<br />
<br />
I used to have ginger hair, then I went<br />
grey and then I became hairless. With ginger<br />
hair, they used to call me "bushy". With grey,<br />
they thought I was a whizz or wizard, and now<br />
I'm called Coot.<br />
and if I display it part of an HTML stream, it looks like this:
The Pie Shop 25 High Street Salisbury Wilts The Pie Shop
25 High Street
Salisbury
Wilts
I used to have ginger hair, then I went
grey and then I became hairless. With ginger
hair, they used to call me "bushy". With grey,
they thought I was a whizz or wizzard, and now
I'm called Coot.
See also ...
offsite link - a further description of the HTML and CSS options, and our
PHP Techniques course and resources (written 2008-10-11, updated 2008-10-12)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
W702 - Web and Intranet - Elements of cascading style sheets [994] Training on Cascading Style Sheets - (2006-12-17)
[999] Cascading and the buses - (2006-12-18)
[1017] css - handling white space and pre - (2007-01-01)
[1019] span and div tags - a css comparison - (2007-01-02)
[1677] CSS training - Cascading Style Sheets (UK course) - (2008-06-15)
[1998] Text on a background image - (2009-01-16)
[4037] Cascading Style Sheets and formatting your web page - (2013-03-10)
Q624 - Object Orientation and General technical topics - HTML - An Overview [433] FTP - how to make the right transfers - (2005-09-01)
[857] Strikingly busy - (2006-09-06)
[1160] HTML - example of a simple web page - (2007-04-24)
[1463] All the special characters in HTML ... - (2007-12-07)
[4034] The VERY basics of a web page ... and web site - (2013-03-09)
H110 - PHP - HTML Web Page Data Handling [50] Current cost in your local currency - (2004-09-16)
[589] Robust PHP user inputs - (2006-02-03)
[789] Hot answers in PHP - (2006-07-02)
[896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
[1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
[1053] Sorting people by name in PHP - (2007-01-26)
[1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
[1169] Emailing as HTML (Web Page) - PHP example - (2007-04-30)
[2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
[2046] Finding variations on a surname - (2009-02-17)
[2107] How to tweet automatically from a blog - (2009-03-28)
[2135] What features does this visitors browser support? (PHP) - (2009-04-22)
[3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
[3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
Some other Articles
23:30 bookings and midnight checkinsSeend, near Melksham, WiltshireWeb Bloopers - good form design - avoiding pitfallsProcessing all files in a directory - PerlText formating for HTML, with PHPCaen Hill and Olivers CastleDont bother to write a Perl programPerl - map to process every member of a list (array)What a shockPerl - Subs, Chop v Chomp, => v ,