Home  >  Article  >  Backend Development  >  Use PHP script to automatically convert plain text files into Web pages_PHP tutorial

Use PHP script to automatically convert plain text files into Web pages_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:05:38781browse

Recently, an old friend of mine called me for help. He has been a journalist for many years and recently won the rights to republish many of his early columns. He wanted to post his work on the Web; but his columns were saved as plain text files, and he had neither the time nor the inclination 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 take care of it," I said, "call me in an hour." Of course, when he called a few hours later, I had already prepared a solution for him. method. It took a little bit of PHP, and I got endless thanks from him and a case of red wine.

So what did I do in this hour? That’s what this article is about. I'll show you how to use PHP to quickly and perfectly convert plain ASCII text into readable HTML markup.

First let’s look at an example of a plain text file that my friend wishes 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/

Pretty standard Text: It has a title, a byline and many paragraphs of text. What you really need to do to convert this document to HTML is to use HTML's line breaks and paragraph tags to preserve the layout of the original text on the Web page. Extraordinary punctuation marks need to be converted into corresponding HTML symbols, and hyperlinks need to be made clickable.

The following PHP code (Listing A) will complete all the above tasks:

Listing A

// 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);

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

// replace special characters with HTML entities
// replace line breaks with

$ html = nl2br(htmlspecialchars($data));

// replace multiple spaces with single spaces
$html = preg_replace('/ss /', ' ', $html);


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445115.htmlTechArticleRecently, an old friend of mine called me for help. He has been a journalist for many years and recently won the rights to republish many of his early columns. He hopes to post his work...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn