Automatically converts plain text into php code on the web page

Source: Internet
Author: User

First, let's look at an example of a plain text file that my friend wants to convert:
Reference content is as follows:
Copy codeThe Code is as follows:
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 comment ation team indicate a high presence of your rophyll in the atmosphere. parameter rophyll, 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. rusel Bunter, the head of ASDA's Mars Colonization Project 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/

A fairly standard text: it has a title, a signature, and many paragraphs. To convert this document into HTML, what really needs to be done is to use the HTML branch and segment mark to keep the layout of the original article on the web page. Special punctuation marks need to be converted into corresponding HTML characters, and hyperlinks need to be clickable.
The following PHP code (List A) completes all the above tasks:
List
Let's take a look at how it works:
Copy codeThe Code is as follows:
<? 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 ('/ss +/', '', $ html );
// Replace URLs with <a href...> elements
$ Html = preg_replace ('/s (w +: //) (S + )/', '<a href = "" target = "_ blank"> </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 ");
?>

The first step is to read the plain ASCII file into a PHP array. This can be done easily through the file () function, which converts each row of the file into an element in an array indexed by numbers.
Then, the title and the author line (I suppose both are the first two lines of the file) are extracted from the array through the array_shift () function and put in a separate variable. The remaining members of the array are connected into a string. This string now includes the body of the entire article.
Special characters such as "'", "<", and ">" in the text are converted to corresponding HTML characters through the htmlspecialchars () function. In order to retain the original format of the article, the branch and segment are converted to HTML through the nl2br () function
Element. Multiple spaces in the article are compressed into one space by simple string replacement.
The URL in the body of the article is detected using a regular expression, with elements on both sides. When the page is displayed in a Web browser, it converts the URL to a clickable hyperlink.
Then, use standard HTML rules to create the output HTML page. The title, author, and text of the article are all formatted using CSS style rules. Although this script does not, you can customize the final page appearance in this place. You can add graphic elements, colors, or other dazzling content to the template.
Once the HTML page is built, it can be sent to a browser or saved as a static file using file_put_contents. To guarantee this, the original file name will be decomposed while the new file name (called filename.html) will be created for the newly created Web page. Then you can publish the Web page to the Web server, save it to the CD, or edit it further.
Note: When using this script to create and save HTML files to the disk, make sure that this script has the write permission on the directory where the files are stored.
As you can see, if you have standard ASCII plain text data files, you can use PHP to quickly convert them into usable Web pages. If you already have a Web site and plan to add a new Web page, it is quite easy to debug the template used by the page builder to adapt it to the appearance of the original Web site.

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.