Webdesign studio WebStudio2U Authorization Write a letter Sitemap

Parsers. Transforming text into an HTML table

Parsers: transforming a text into an HTML table In the process of website development web developers often face a problem of correct transformation of text into HTML tables. In this article the specialists of WebStudio2U will share their experience of coding a simple text parser. We will tell you how to write a simple application using Delphi programming language in 15 minutes. This application will transform a text with delimiters into an HTML table. This article is intended for the users who have minimal knowledge of Delphi.

First of all let us place two Memo (mmo1 and mmo2) components and one Button (btn1) component into the form. Now let us proceed to editing the values of the component parameters using the Object Inspector panel. You should set the ScrollBars parameter of TMemo class components to ssBoth and edit the value of the Lines parameter – erase the name of the component from the text. Then change the value of the Caption parameter for the button into “Create a table” (see image 1).

Image 1Parsers: transforming a text into an HTML table

Click the btn1 twice and replace the procedure by the procedure given below:

To make this code work you should add the function which will divide the line into the line array with the delimiter. You should insert the code of the function above the code of the procedure that processes the click on the button.

That's all! Now you can compile the program (F9).

It is presumed that the source text will be placed into mmo1, and after the processing the HTML table will be output into mmo2.

To make it more convenient we have also added an optional handler for Ctrl+A combination press on the mmo2 component (onKeyDown event).

The handler for the onKeyDown event of the mmo2 component itself

Such a simple but very useful application may be useful, for example, if you need to transform the keywords statistics provided by the search engines into an HTML table. First you should copy the data to Excel or other similar program via the clipboard, prepare the information there, then paste it into mmo1 (when the text is copied to Excel it is automatically transformed into the text with a delimiter – tabulation character) and transform it into an HTML table with the help of the program.

If you have any questions, contact us – we will be glad to answer!

P.S. To make the procedure of copying to clipboard work you will need to add the Clipbrd module into the list of connected modules (uses list in the very beginning of the program code).

TAGS
parser, html table, text to table, delphi, text with delimiters, code, program, application, text parser