Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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
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)


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 ,
4759 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, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.info/mouth/1831_Tex ... h-PHP.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb