Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/1831_Tex ... h-PHP.html

Our email: info@wellho.net • Phone: 01144 1225 708225

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 &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
(written 2008-10-11, updated 2008-10-12)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H110 - PHP - HTML Web Page Data Handling
  [3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
  [3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
  [2135] What features does this visitors browser support? (PHP) - (2009-04-22)
  [2107] How to tweet automatically from a blog - (2009-03-28)
  [2046] Finding variations on a surname - (2009-02-17)
  [2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
  [1169] Emailing as HTML (Web Page) - PHP example - (2007-04-30)
  [1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
  [1053] Sorting people by name in PHP - (2007-01-26)
  [1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
  [896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
  [789] Hot answers in PHP - (2006-07-02)
  [589] Robust PHP user inputs - (2006-02-03)
  [50] Current cost in your local currency - (2004-09-16)

Q624 - Object Orientation and General technical topics - HTML - An Overview
  [4034] The VERY basics of a web page ... and web site - (2013-03-09)
  [1463] All the special characters in HTML ... - (2007-12-07)
  [1160] HTML - example of a simple web page - (2007-04-24)
  [857] Strikingly busy - (2006-09-06)
  [433] FTP - how to make the right transfers - (2005-09-01)

W702 - Web and Intranet - Elements of cascading style sheets
  [4037] Cascading Style Sheets and formatting your web page - (2013-03-10)
  [1998] Text on a background image - (2009-01-16)
  [1677] CSS training - Cascading Style Sheets (UK course) - (2008-06-15)
  [1019] span and div tags - a css comparison - (2007-01-02)
  [1017] css - handling white space and pre - (2007-01-01)
  [999] Cascading and the buses - (2006-12-18)
  [994] Training on Cascading Style Sheets - (2006-12-17)


Back to
Caen Hill and Olivers Castle
Previous and next
or
Horse's mouth home
Forward to
Processing all files in a directory - Perl
Some other Articles
23:30 bookings and midnight checkins
Seend, near Melksham, Wiltshire
Web Bloopers - good form design - avoiding pitfalls
Processing all files in a directory - Perl
Text formating for HTML, with PHP
Caen Hill and Olivers Castle
Dont bother to write a Perl program
Perl - map to process every member of a list (array)
What a shock
Perl - Subs, Chop v Chomp, => v ,
4300 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, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 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).

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1831_Tex ... h-PHP.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat