Recently, an old friend of mine called me for help. He has been a journalist for many years and recently gained the right to re-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 person in his phone book who was proficient in computers, he called me to see if I could help him.
"Let me handle it," I said, "Call me back in one hours." "Of course, when he called me a few hours later, I had prepared a solution for him." It took a little bit of PHP, and I harvested his endless thanks and a box of red wine.
So what have I done in these one hours? This is the content of this article. I'll show you how to use PHP to quickly transform pure ASCII text into a readable HTML tag.
First, let's look at an example of a plain text file my friend wants to convert:
Green for mars!
John R. Doe
The idea of little green men from Mars, long a staple of the science fiction, may soon turn off to being 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 the mean for? Well, it means blah blahblah ...
Track follow-ups to this stories 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, a signature, and many paragraphs of text. The real thing about converting this document into HTML is to keep the original layout on the Web page using the HTML branch and fragment tags. Special punctuation needs to be converted into corresponding HTML symbols, hyperlinks need to become clickable.
The following PHP code (List A) will complete all of the above tasks:
List 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);
Join remaining 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);
http://www.bkjia.com/PHPjc/445115.html www.bkjia.com true http://www.bkjia.com/PHPjc/445115.html techarticle recently, an old friend of mine called me for help. He has been a journalist for many years and recently gained the right to re-publish many of his early columns. He wants to put his work on the stick ...