PHP code to automatically convert plain text to Web pages _php tutorial

Source: Internet
Author: User
First, let's look at an example of a plain text file my friend wants to convert:
The following is the referenced content:
Copy CodeThe code is as follows:
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
Let's take a look at how it works:
Copy CodeThe code is as follows:
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);
Replace URLs with elements
$html = Preg_replace ('/s (w+://) (s+)/', ' ", $html);
Start building Output page
Add Page Header
$output =<<< HEADER





HEADER;
Add page Content
$output. = "$slug";
$output. = "by $byline

";
$output. = "$html";
Add page Footer
$output .=<<< FOOTER


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 by using the file () function, which converts each line of a file into an element in a numerically indexed array.
Then, the title and the author line (which I assume both are the first two lines of the file) are extracted from the array by the Array_shift () function and placed in a separate variable. The remaining members of the array are then concatenated into a string. This string now includes the text of the whole article.
Special symbols such as "'", "<" and ">" in the body of the article are converted to corresponding HTML symbols through the Htmlspecialchars () function. To preserve the original format of the article, branches and segments are converted to HTML through the NL2BR () function.
Elements. In the middle of the article, multiple spaces are compressed into a space by simple string substitution.
The URL in the body of the article is detected with a regular expression, with elements on either side. When the page is displayed in a Web browser, it transforms the URL into a clickable hyperlink.
The output HTML page is then created with standard HTML rules. The title, author, and body of the article are formatted with CSS style rules. Although this script does not do this, you can customize the appearance of the final page in this place, and you can add graphic elements, colors, or other dazzling content to the template.
Once the HTML page is built, it can be sent to the browser or saved as a static file with File_put_contents (). Note that, at the time of saving, the original file name will be decomposed, and a new filename (called filename.html) will be created for the newly created Web page. You can then publish the Web page to a Web server, save it to a disc, or edit it further.
Note: When using this script to create and save an HTML file to disk, you need to make sure that the script has write access to the directory where the file is saved.
As you can see, if you have an ASCII plain text data file in standard format, you could convert it to a Web page that can be used quite quickly with PHP. If you already have a Web site and plan to add a new Web page, it's fairly easy to debug the template used by the page builder to make it look like the original web site.

http://www.bkjia.com/PHPjc/320539.html www.bkjia.com true http://www.bkjia.com/PHPjc/320539.html techarticle first, let's take a look at an example of a plain text file that my friend wishes to convert: The following is the reference: the copy Code code is as follows: Green for mars! John R. Doe the idea of little ...

  • 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.