当前位置:首页 > 网络编程 > WEB编程 > PHP > 用PHP自动把纯文本转换成Web页面

用PHP自动把纯文本转换成Web页面

点击次数:55 次 发布日期:2008-11-07 07:56:00 作者:源代码网
源代码网推荐

源代码网整理以下最近,我的一个老朋友向我打电话求助。他从事记者的职业有多年了,最近获得了重新出版他的很多早期专栏的权利。他希望把他的作品贴在Web上;但是他的专栏都是以纯文本文件的形式保存的,而且他既没有时间也不想去为了把它们转换成为Web页面而学习HTML的知识。由于我是他电话本里唯一一个精通计算机的人,所以他打电话给我看我是否能够帮帮他。

源代码网整理以下“让我来处理吧,”我说:“一个小时以后再给我打电话。”当然了,当他几个小时以后打电话过来,我已经为他准备好了解决的方法。这需要用到一点点PHP,而我收获了他没完没了的感谢和一箱红酒。

源代码网整理以下那么我在这一个小时里做了些什么呢?这就是本篇文章的内容。我将告诉你如何使用PHP来快速将纯ASCII文本完美地转换成为可读的HTML标记。

源代码网整理以下首先让我们来看一个我朋友希望转换的纯文本文件的例子:

源代码网整理以下

以下为引用的内容:

源代码网整理以下  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/

源代码网整理以下相当标准的文本:它有一个标题、一个署名和很多段的文字。把这篇文档转换成为HTML真正需要做的是使用HTML的分行和分段标记把原文的布局保留在Web页面上。特殊的标点符号需要被转换成为对应的HTML符号,超链接需要变得可以点击。

源代码网整理以下下面的PHP代码(列表A)就会完成上面所有的任务:

源代码网整理以下列表A

源代码网整理以下让我们来看看它是如何工作的:

源代码网整理以下

以下为引用的内容:

源代码网整理以下<?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");
?>

源代码网整理以下 源代码网供稿.

网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华