Main Content

Text formating for HTML, with PHP

Archive - Originally posted on "The Horse's Mouth" - 2008-10-11 06:40:37 - Graham Ellis

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 &nbsp; 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 &nbsp; - 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