Automatically convert plain text files to Web pages with PHP scripts

Source: Internet
Author: User
Tags php code

Recently, an old friend of mine called me for help. He has worked as a journalist for many years and recently gained the right to publish many of his early columns. He wanted to put his work on the web, but his columns were saved as plain text files, and he had neither the time nor the desire to learn HTML in order to convert them into Web pages. Since I was the only computer-savvy person in his phone book, he called me to see if I could help him.

"Let me handle it," I said, "and call me back in one hours." "Of course, when he called me a few hours later, I had prepared a solution for him," he said. It takes a little bit of PHP, and I harvest his endless thanks and a box of red wine.

So what have I done in the one hours? This is the content of this article. I'll show you how to use PHP to quickly transform pure ASCII text into readable HTML markup.

First, let's look at an example of a plain text file that my friend wants 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 FA Ct.

Recent samples sent by the latest Mars exploration team, indicate a high presence of chlorophyll in the atmosphere. Chlorophyll, you'll recall, is what makes plants green. It's quite likely, therefore, that organisms on Mars would 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? So, it means blah blahblah ...

Track follow-ups to the story online at http://www.mars-connect.dom/. To-pictures of the latest samples, log on to http://www.asdamcp.dom/galleries/220/

Fairly standard text: it has a title, a signature, and many paragraphs of text. What you really need to do to translate this document into HTML is to keep the layout of the original text on a Web page using HTML's branch and paragraph tags. Extraordinary punctuation needs to be converted into corresponding HTML symbols, and hyperlinks need to be clickable.

The following PHP code (listing a) completes all of the above tasks:

List A

<?php
Set source file name and path
$source = "Toi200686.txt";

Read raw text as array
$raw = File ($source) or Die ("Cannot read file");

Retrieve 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 ('/ss/', ', $html);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.