|
Text formating for HTML, with PHP
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 06:40:37)
Associated topics are indexed under H110 - PHP - HTML Web Page Data HandlingQ624 - Object Orientation and General technical topics - HTML - An OverviewW702 - Web and Intranet - Elements of cascading style sheets
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 ,
|
2259 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|