Converting Text to HTML with PHP

Google+ Pinterest LinkedIn Tumblr +

Introduction
You can type an article into a word processor. That word processor would have a
program you can use to convert the article into an HTML document, to be displayed on
the web. The problem is that the resulting HTML document might have a lot of extra
code you do not want, and the conversion may not be good. The extra code makes the
resulting HTML document, long; you may not even understand some of the extra code
and it becomes more difficult to edit the resulting HTML document. However, if you
understand PHP, you would be able to produce the corresponding HTML document with
no extra code, using a PHP program. The process is this: you save the document from the
word processor as text-only file. Then you write PHP code to produce the HTML
document with no extra code added. In this article I show you how to produce some
HTML code, with no extra code added, using PHP, from a text file.

You may argue that there are ready-made software packages in which you can type your
article directly and they will produce the HTML document for you. With these packages,
the problem of extra code and editing still occur, and they do not give you a lot of
flexibility with what you want to do with the resulting HTML code.

You need basic knowledge in HTML, PHP, PHP File System and PHP Regular
Expressions to appreciate this article.

Note: If you cannot see the code or if you think anything is missing (broken link, image
absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest
problem you have about what you are reading.

Overall Process
You type your article, essay, news, whatever, in your word processor. You type the
article in your word processor instead of a text editor, because the word processor will
allow you to check spelling and grammar and do some formatting; the formatting should
be minimum in this case. Then you save the article as text-only file. This would save the
file without any word processor code. Almost all word processors today allow you to
save their content as text-only file. You then use the PHP program you have written to
convert the text file into an HTML document. In this article the PHP program I will give
you will convert the text file into a simple HTML code and not the complete HTML
document. You can then copy and paste the simple HTML code into a DIV element of an
HTML document template.

The PHP Program code Segments
The PHP program I present is short, but very involving. It has six code segments.

The first code segment obtains the path and name of the text file from a web page. This
segment copies the file from the directory at the server and assigns the file content to a
PHP variable. This variable now holds the file content in the form of a string.

When you type an article in a word processor, it may have characters such as ‘<‘ that
have special meaning to HTML. The next (second) code segment in the PHP program
replaces all such characters in the file string with HTML entities. < for example would be
replaced by, “<” (or some equivalent).

When you type an article such as arithmetic or computer programming or poetry article,
you would use the spacebar key to produce spaces, in order to give some formatting to a
section of text. Ideally, for the HTML code, such a section of text should be in an HTML
PRE element. The PRE element normally displays with font that is different from the rest
of the text. This may not be too bad. The really problem with the PRE element is that it
comes with a fixed width, depending on the lengths of line it contains. The width may be
too long and at the web page the user would have to be scrolling left and right in order to
see bids of text. Always design your web page in such a way that the user will scroll up
and down but not left and right; it is very inconvenient to be scrolling left and right. For
this reason and for the purpose of this article, you should not have such formatting in an
HTML PRE element, for the HTML code..

A simple and effective solution to this problem is to replace the occurrence of more than
one consecutive spaces with ” ” entities. In this way, if your lines for the expected
formatted text are too long, they will wrap at the browser. If you use the PRE element,
these lines will not wrap at the browser and the user might have to be scrolling left and
right.

When you are typing a document in your word processor, for this PHP program, do not
use the tab key. The tab key does not have any equivalent HTML code, today. Use the
spacebar key when you want a long space. When you type the spacebar key once, you
have a single space. When you type it several times, you have a space equivalent to
pressing the tab key.

The third code segment of the PHP program replaces all consecutive single spaces with
” ” entities.

The fourth code segment puts HTML line break elements,
, where you have new
line characters in the file string.

The fifth code segment echoes the file string content to the browser, so that you see what
has been done.

The last code segment, copies the string content to disk, replacing the original file with
the HTML version as a simple HTML code. This code does not have the BODY, HEAD,
title elements, etc. You can copy the code and put it in a DIV element in some HTML
template.

So, what the program does is that it replaces all the new line characters of the text file
with the HTML line break element and then it replaces any consecutive single spaces
with the corresponding HTML entities. Even though this result looks small, it gives you
an acceptable presentation of the article.

Well, that is it for the simple PHP program. You can download the program free from
http://www.cool-mathematics.biz/downloads/Converting-Text-to-HTML-with-PHP.htm
.

You would want for your published article, more than what this program does. However,
this is the minimum any such program should do. Additionally, you might want the
program to bold headings. You might want the program to produce HTML tables from
text of a particular format in the article. You might want the program to insert images.
You might want the program to include advertisement code. You might want the program
to convert web addresses to links and do other things. You may even want web
development where a writer would type his article on a web page, do word processing on
that web page and then submit what he has typed to appear as a well-presented HTML
document at some site. Well, if you want more than what this simple program does, and
you cannot write your own or you do not have the time to write an elaborated program,
just contact me at, forchatrans@yahoo.com .

Chrys

Share.

About Author

Leave A Reply