


Use PHP script to automatically convert plain text files into Web pages_PHP tutorial
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);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
