Thursday, November 29, 2007

Text File To Web Page Automatically

Transform plain text files into Web pages automatically with this PHP script
By Contributor Melonfire, TechRepublic | 2007/01/08 11:26:02

Print this | E-mail this | Leave a comment | digg this | del.ici.ous

Change the text size: a | A

Recently, an old friend of mine rang me up to ask for help. He'd been working as a journalist for many years, and had recently received reprint rights to a number of his earlier columns.

He was eager to publish his past work on the Web; however, his columns were all saved as plain-text files and he had neither the time nor the inclination to learn HTML and convert them to Web pages. Since I was the only geek in his phone book, he'd called me to see if I could help him.

"Let me take care of it", I said. "Call me back in an hour", I said. And sure enough, when he called back a couple of hours later, I had a solution waiting for him. It involved a little bit of PHP, and it earned me his eternal thanks and a crate of wine.

So what did I do in that hour? That's where this article comes in. I'm going to show you how you can use PHP to quickly transform plain ASCII text into perfectly readable HTML markup.

To begin, let's look at an example of one of the raw text files my friend wanted to convert:

Green for Mars!
John R. Doe

The idea of little green men from Mars, long a staple of science fiction, may soon
turn out to be less fantasy and more fact.

Recent samples sent by the latest Mars exploration team indicate a high presence
of chlorophyll in the atmosphere. Chlorophyll, you will recall, is what makes
plants green. It's quite likely, therefore, that organisms on Mars will have,
through continued exposure to the green stuff, developed a greenish tinge on their
outer exoskeleton.

An interview with Dr. Rushel Bunter, the head of ASDA's Mars Colonization Project
blah blah...

What does this mean for you? Well, it means blah blahblah...

Track follow-ups to this story online at http://www.mars-connect.dom/.

To see pictures of the latest samples, log on to http://www.asdamcp.dom/galleries/220/

Fairly standard text: it has a title (or "slug"), a byline, and many paragraphs of text. All that's really needed to transform this document into HTML is to use HTML line and paragraph break markers to preserve the original layout on a Web page. Special punctuation characters need to be converted into their HTML equivalents, and hyperlinks need to be made clickable.

Here's the PHP code (Listing A) to accomplish all of the above:

<?php
// set source file name and path
$source = "toi200686.txt";

// read raw text as array
$raw = file($source) or die("Cannot read file");

// retrieve first and second lines (title and author)
$slug = array_shift($raw);
$byline = array_shift($raw);

// join remaining data into string
$data = join('', $raw);

// replace special characters with HTML entities
// replace line breaks with <br />
$html = nl2br(htmlspecialchars($data));

// replace multiple spaces with single spaces
$html = preg_replace('/\s\s+/', ' ', $html);

// replace URLs with <a href...> elements
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' <a href="\\1\\2" target="_blank">\\1\\2</a>', $html);

// start building output page
// add page header
$output =<<< HEADER
<html>
<head>
<style>
.slug {font-size: 15pt; font-weight: bold}
.byline { font-style: italic }
</style>
</head>
<body>
HEADER;

// add page content
$output .= "<div class='slug'>$slug</div>";
$output .= "<div class='byline'>By $byline</div><p />";
$output .= "<div>$html</div>";

// add page footer
$output .=<<< FOOTER
</body>
</html>
FOOTER;

// display in browser
echo $output;

// AND/OR

// write output to a new .html file
file_put_contents(basename($source, substr($source, strpos($source, '.'))) . ".html", $output) or die("Cannot write file");
?>

Let's see how this works:

1. The first step is to read the raw ASCII file into a PHP array. This is easily accomplished with the file() function, which turns every line of the file into an element of a numerically-indexed array.
2. Next, the title and author lines (I assume these are the first two lines of the file) are extracted from the array into separate variables using the array_shift() function. The remaining members of the array are then concatenated into a single string. This string will now contain the entire body of the article.
3. Special characters like ', < and > within the body are converted into their HTML equivalents using the htmlspecialchars() function. To preserve the original formatting of the article, line and paragraph breaks are converted into HTML
elements with the nl2br() function. Multiple spaces within the article body are compressed into a single space using simple string replacement.
4. URLs within the body are detected using regular expressions, and are surrounded by elements. This turns the URLs into clickable hyperlinks when the page is viewed in a Web browser.
5. The output HTML page is then constructed using standard HTML rules. The article title, author and body are formatted using CSS style rules. Although this script doesn't do it, this is the point at which you would customize the appearance of the final page, perhaps by adding graphical elements, colors or other whiz-bangs to the template.
6. Once the HTML page has been constructed, it can be sent to the browser or saved to a static file with file_put_contents(). Note that when saving, the original file name is decomposed and a new file (named filename.html) is created for the newly-minted Web page. You can then publish this Web page to a Web server, save it to a CD-ROM or edit it further.

Note: When using this script to create and save HTML files to disk, ensure that the script has write privileges on the directory to which the files are being saved.

As you can see, assuming you have ASCII plain-text data files in a standard format, you can convert them fairly quickly into usable Web pages with PHP. And if you have an existing Web site into which you plan to inject your new Web pages, it's also quite easy to tweak the template used by the page generator to match the look and feel of your existing Web site. So go on, try it out for yourself!

http://www.builderau.com.au/program/php/soa/Transform_plain_text_files_into_Web_pages_automatically_with_this_PHP_script/0,339028448,339272897,00.htm

Wednesday, November 28, 2007

Rosolution Adjust In Delphi

Another to change resolution in delphi, I used to apply is the following

function NewRes(XRes, YRes: DWORD): Integer;
var
DevMode: TDeviceMode;
begin
EnumDisplaySettings(nil, 0, DevMode);
DevMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT or
DM_DISPLAYFREQUENCY;
DevMode.dmPelsWidth := XRes;
DevMode.dmPelsHeight := YRes;
//DevMode.dmDisplayFrequency := Frequency;
Result := ChangeDisplaySettings(DevMode, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
w:=Screen.Width;h:=Screen.Height;
if NewRes(800, 600) = DISP_CHANGE_SUCCESSFUL
then
ShowMessage('Resolution changed!');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if NewRes(w, h) = DISP_CHANGE_SUCCESSFUL
then
ShowMessage('Resolution changed Back!');
end;

Change Resolution In Delphi

How to change the screen resolution in delphi? Here is the function maybe usefull

function NewRes(XRes, YRes: DWORD): Integer;
var
DevMode: TDeviceMode;
begin
EnumDisplaySettings(nil, 0, DevMode);
DevMode.dmFields := DM_PELSWIDTH or DM_PELSHEIGHT or
DM_DISPLAYFREQUENCY;
DevMode.dmPelsWidth := XRes;
DevMode.dmPelsHeight := YRes;
//DevMode.dmDisplayFrequency := Frequency;
Result := ChangeDisplaySettings(DevMode, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
w:=Screen.Width;h:=Screen.Height;
if NewRes(800, 600) = DISP_CHANGE_SUCCESSFUL
then
ShowMessage('Resolution changed!');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if NewRes(w, h) = DISP_CHANGE_SUCCESSFUL
then
ShowMessage('Resolution changed Back!');
end;

Monday, November 26, 2007

Store Report Templates In Blob

How to store report templates in blob fields in database.
Answer:

Sometimes it is useful to keep report templates in blob fields in database. If you use EK RTF report component for Delphi you may use blob field to store report template in it.

Procedures below are using TBlobStream for input/output operations.

Store template in database:

procedure StoreTemplate(Fname:string);
var BS:TBlobStream;
begin
ReadTempFile(FName);
Table1.insert;
BS:=TBlobStream.create(Table1.FieldByName('Field1')as TBlobField,bmWrite);
BS.Write((PFile)^,flent);
Table1.Post;
Bs.Free;
end;

procedure ReadTempFile(FInFile:string);
var ActualRead : cardinal;
FileHandle: Integer;
SearchRec : TsearchRec;
size:integer;
flent:longint;
begin
flent:=-1;
if FindFirst(FInFile, faAnyFile, SearchRec)=0 then
begin
size:=SearchRec.Size;
GetMem(PFile,size+2);
FileHandle := FileOpen(FInFile, fmOpenRead);
ActualRead :=FileRead(FileHandle, PFile^, size);
FileClose(FileHandle);
flent:=ActualRead;
end;
if flent=0 then
begin showmessage('Can''t read file'+FInFile); end;
FindClose(SearchRec);
end;

Read template from database:

procedure ReadTemplate;
var BS:TBlobStream;
buffer:pointer;
size:longint;
begin
BS:=TBlobStream.create(Table1Field1,bmRead);
size:=BS.Seek(0,soFromEnd);
BS.Seek(0,soFromBeginning);
GetMem(Buffer,size);
BS.Seek(0,0);
BS.Read((Buffer)^,size);
BS.Free;
//Use method SetTemplateBuffer to assign template for EK RTF component
EKRTF1.SetTemplateBuffer(Buffer, Size);
end;

How To Use BLOB Type in Delphi

Many RDBMS usually complated with BLOB datatype. As far as I know there are many database supported blob already including the free version, which is firebird, mysql, postgresql, oracle and mssqlserver.

Blob type can be used to restore file such as image, e-book, exe file etc. And according to me, it can be used to make an update software version.

In delphi to save and load blob type is as the following:

//Save to blobstream and load
// To save a file to BLOB:
procedure TForm1.Button1Click(Sender: TObject);
var
//blob: TBlobStream;
blob, fs: TStream;
begin
blob := yourDataset.CreateBlobStream(yourDataset.FieldByName('YOUR_BLOB'), bmWrite);
try
blob.Seek(0, soFromBeginning);
fs := TFileStream.Create('c:\your_name.doc', fmOpenRead or
fmShareDenyWrite);
try
blob.CopyFrom(fs, fs.Size)
finally
fs.Free
end;
finally
blob.Free
end;
end;

// To load from BLOB:

procedure TForm1.Button1Click(Sender: TObject);
var
//blob: TBlobStream;
blob: TStream;
begin
blob := yourDataset.CreateBlobStream(yourDataset.FieldByName('YOUR_BLOB'), bmRead);
try
blob.Seek(0, soFromBeginning);

with TFileStream.Create('c:\your_name.doc', fmCreate) do
try
CopyFrom(blob, blob.Size)
finally
Free
end;
finally
blob.Free
end;
end;

Friday, November 23, 2007

Clone Record (Delphi)

procedure CloneRecord(Dataset: TDataSet);
var
aField : Variant;
i : Integer;
begin

// Create a variant Array
aField := VarArrayCreate([0,DataSet.Fieldcount-1],VarVariant);

// read values into the array
for i := 0 to (DataSet.Fieldcount-1) do
aField[i] := DataSet.fields[i].Value ;

DataSet.Append ;

// Put array values into new the record
for i := 0 to (DataSet.Fieldcount-1) do
DataSet.fields[i].Value := aField[i] ;

end;