将RTF格式的文件转成HTML并在网页中显示的代码
来源:互联网 发布:java开发小游戏教程 编辑:程序博客网 时间:2024/05/22 03:06
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
它是这样工作的,将一个RTF文件上传,然后转成HTML显示出来,代码有点复杂,teaman还要好好研究,好象中文版有点问题。
<HTML>
<body>
<?
if(!($userfile)) {
?>
<form enctype="multipart/form-data" action="<?print($PHP_SELF);?>" method=post>
<input type=hidden name="max_file_size" value=2000>
Send this file: <input name=userfile type=file>
<input type=submit value=Upload>
</form>
</body>
</HTML>
<?
exit;
}
function ProcessTags($tags, $line) {
$HTML = "";
global $color;
global $size;
global $bullets;
// Remove spaces.
$tags = trim($tags);
// Found the beginning of the bulleted l
// ist.
if(ereg("///pnindent", $tags)) {
$HTML .= "<ul><li>";
$bullets += $line;
$tags = ereg_replace("///par", "", $tags);
$tags = ereg_replace("///(tab)", "", $tags);
}
if($line - $bullets == 0) {
$tags = ereg_replace("///par", "", $tags);
}
elseif($line - $bullets == 1) {
if(ereg("///pntext", $tags)) {
$HTML .= "<li>";
$tags = ereg_replace("///par", "", $tags);
$tags = ereg_replace("///(tab)", "", $tags);
$bullets++;
}
else {
$HTML .= "</ul>";
$bullets = 0;
}
}
// Convert Bold.
if(ereg("///b0", $tags)){ $HTML .= "</b>"; }
elseif(ereg("///b", $tags)) { $HTML .= "<b>"; }
// Convert Italic.
if(ereg("///i0", $tags)){ $HTML .= "</i>"; }
elseif(ereg("///i", $tags)) { $HTML .= "<i>"; }
// Convert Underline.
if(ereg("///ulnone", $tags)){ $HTML .= "</u>"; }
elseif(ereg("///ul", $tags)){ $HTML .= "<u>"; }
// Convert Alignments.
if(ereg("///pard///qc", $tags)) { $HTML .= "<div align=center>"; }
elseif(ereg("///pard///qr", $tags)) { $HTML .= "<div align=right>"; }
elseif(ereg("///pard", $tags)){ $HTML .= "<div align=left>"; }
// Remove /pard from the tags so it does
// n't get confused with /par.
$tags = ereg_replace("///pard", "", $tags);
// Convert line breaks.
if(ereg("///par", $tags)){ $HTML .= "<br>"; }
// Use the color table to capture the fo
// nt color changes.
if(ereg("///cf[0-9]", $tags)) {
global $fcolor;
$numcolors = count($fcolor);
for($i = 0; $i < $numcolors; $i++) {
$test = "///cf" . ($i + 1);
if(ereg($test, $tags)) {
$color = $fcolor[$i];
}
}
}
// Capture font size changes.
if(ereg("///fs[0-9][0-9]", $tags, $temp)) {
$size = ereg_replace("///fs", "", $temp[0]);
$size /= 2;
if($size <= 10) { $size = 1; }
elseif($size <= 12) { $size = 2; }
elseif($size <= 14) { $size = 3; }
elseif($size <= 16) { $size = 4; }
elseif($size <= 18) { $size = 5; }
elseif($size <= 20) { $size = 6; }
elseif($size <= 22) { $size = 7; }
else{ $size = 8; }
}
// If there was a font color or size cha
// nge, change the font tag now.
if(ereg("(///cf[0-9])||(///fs[0-9][0-9])", $tags)) {
$HTML .= "</font><font size=$size color=$color>";
}
// Replace /tab with alternating spaces
// and nonbreakingwhitespaces.
if(ereg("///(tab)", $tags)) { $HTML .= " "; }
return $HTML;
}
function ProcessWord($word) {
// Replace // with /
$word = ereg_replace("[//]{2,}", "//", $word);
// Replace /{ with {
$word = ereg_replace("[//][/{]", "/{", $word);
// Replace /} with }
$word = ereg_replace("[//][/}]", "/}", $word);
// Replace 2 spaces with one space.
$word = ereg_replace(" ", " ", $word);
return $word;
}
$color = "000000";
$size = 1;
$bullets = 0;
// Read the uploaded file into an array.
//
$RTFile = file($userfile);
$fileLength = count($RTFile);
// Loop through the rest of the array
for($i = 1; $i < $fileLength; $i++) {
/*
** If the line contains "/colortbl" then we found the color table.
** We'll have to split it up into each individual red, green, and blue
** Convert it to hex and then put the red, green, and blue back together.
** Then store each into an array called fcolor.
*/
if(ereg("^/{///colortbl", $RTFile[$i])) {
// Split the line by the backslash.
$colors = explode("//", $RTFile[$i]);
$numOfColors = count($colors);
for($k = 2; $k < $numOfColors; $k++) {
// Find out how many different colors th
// ere are.
if(ereg("[0-9]+", $colors[$k], $matches)) {
$match[] = $matches[0];
}
}
// For each color, convert it to hex.
$numOfColors = count($match);
for($k = 0; $k < $numOfColors; $k += 3) {
$red = dechex($match[$k]);
$red = $match[$k] < 16 ? "0$red" : $red;
$green = dechex($match[$k + 1]);
$green = $match[$k +1] < 16 ? "0$green" : $green;
$blue = dechex($match[$k + 2]);
$blue = $match[$k + 2] < 16 ? "0$blue" : $blue;
$fcolor[] = "$red$green$blue";
}
$numOfColors = count($fcolor);
}
// Or else, we parse the line, pulling o
// ff words and tags.
else {
$token = "";
$start = 0;
$lineLength = strlen($RTFile[$i]);
for($k = 0; $k < $lineLength; $k++) {
if($RTFile[$i][$start] == "//" && $RTFile[$i][$start + 1] != "//") {
// We are now dealing with a tag.
$token .= $RTFile[$i][$k];
if($RTFile[$i][$k] == " ") {
$newFile[$i] .= ProcessTags($token, $i);
$token = "";
$start = $k + 1;
}
elseif($RTFile[$i][$k] == "/n") {
$newFile[$i] .= ProcessTags($token, $i);
$token = "";
}
}
elseif($RTFile[$i][$start] == "{") {
// We are now dealing with a tag.
$token .= $RTFile[$i][$k];
if($RTFile[$i][$k] == "}") {
$newFile[$i] .= ProcessTags($token, $i);
$token = "";
$start = $k + 1;
}
}
else {
// We are now dealing with a word.
if($RTFile[$i][$k] == "//" && $RTFile[$i][$k + 1] != "//" && $RTFile[$i][$k - 1] != "//") {
$newFile[$i] .= ProcessWord($token);
$token = $RTFile[$i][$k];
$start = $k;
}
else {
$token .= $RTFile[$i][$k];
}
}
}
}
}
$limit = sizeof($newFile);
for($i = 0; $i < $limit; $i++) {
print("$newFile[$i]/n");
}
?>
</body>
</HTML>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
将RTF格式的文件转成HTML并在网页中显示的代码 它是这样工作的,将一个RTF文件上传,然后转成HTML显示出来,代码有点复杂,teaman还要好好研究,好象中文版有点问题。
<HTML>
<body>
<?
if(!($userfile)) {
?>
<form enctype="multipart/form-data" action="<?print($PHP_SELF);?>" method=post>
<input type=hidden name="max_file_size" value=2000>
Send this file: <input name=userfile type=file>
<input type=submit value=Upload>
</form>
</body>
</HTML>
<?
exit;
}
function ProcessTags($tags, $line) {
$HTML = "";
global $color;
global $size;
global $bullets;
// Remove spaces.
$tags = trim($tags);
// Found the beginning of the bulleted l
// ist.
if(ereg("///pnindent", $tags)) {
$HTML .= "<ul><li>";
$bullets += $line;
$tags = ereg_replace("///par", "", $tags);
$tags = ereg_replace("///(tab)", "", $tags);
}
if($line - $bullets == 0) {
$tags = ereg_replace("///par", "", $tags);
}
elseif($line - $bullets == 1) {
if(ereg("///pntext", $tags)) {
$HTML .= "<li>";
$tags = ereg_replace("///par", "", $tags);
$tags = ereg_replace("///(tab)", "", $tags);
$bullets++;
}
else {
$HTML .= "</ul>";
$bullets = 0;
}
}
// Convert Bold.
if(ereg("///b0", $tags)){ $HTML .= "</b>"; }
elseif(ereg("///b", $tags)) { $HTML .= "<b>"; }
// Convert Italic.
if(ereg("///i0", $tags)){ $HTML .= "</i>"; }
elseif(ereg("///i", $tags)) { $HTML .= "<i>"; }
// Convert Underline.
if(ereg("///ulnone", $tags)){ $HTML .= "</u>"; }
elseif(ereg("///ul", $tags)){ $HTML .= "<u>"; }
// Convert Alignments.
if(ereg("///pard///qc", $tags)) { $HTML .= "<div align=center>"; }
elseif(ereg("///pard///qr", $tags)) { $HTML .= "<div align=right>"; }
elseif(ereg("///pard", $tags)){ $HTML .= "<div align=left>"; }
// Remove /pard from the tags so it does
// n't get confused with /par.
$tags = ereg_replace("///pard", "", $tags);
// Convert line breaks.
if(ereg("///par", $tags)){ $HTML .= "<br>"; }
// Use the color table to capture the fo
// nt color changes.
if(ereg("///cf[0-9]", $tags)) {
global $fcolor;
$numcolors = count($fcolor);
for($i = 0; $i < $numcolors; $i++) {
$test = "///cf" . ($i + 1);
if(ereg($test, $tags)) {
$color = $fcolor[$i];
}
}
}
// Capture font size changes.
if(ereg("///fs[0-9][0-9]", $tags, $temp)) {
$size = ereg_replace("///fs", "", $temp[0]);
$size /= 2;
if($size <= 10) { $size = 1; }
elseif($size <= 12) { $size = 2; }
elseif($size <= 14) { $size = 3; }
elseif($size <= 16) { $size = 4; }
elseif($size <= 18) { $size = 5; }
elseif($size <= 20) { $size = 6; }
elseif($size <= 22) { $size = 7; }
else{ $size = 8; }
}
// If there was a font color or size cha
// nge, change the font tag now.
if(ereg("(///cf[0-9])||(///fs[0-9][0-9])", $tags)) {
$HTML .= "</font><font size=$size color=$color>";
}
// Replace /tab with alternating spaces
// and nonbreakingwhitespaces.
if(ereg("///(tab)", $tags)) { $HTML .= " "; }
return $HTML;
}
function ProcessWord($word) {
// Replace // with /
$word = ereg_replace("[//]{2,}", "//", $word);
// Replace /{ with {
$word = ereg_replace("[//][/{]", "/{", $word);
// Replace /} with }
$word = ereg_replace("[//][/}]", "/}", $word);
// Replace 2 spaces with one space.
$word = ereg_replace(" ", " ", $word);
return $word;
}
$color = "000000";
$size = 1;
$bullets = 0;
// Read the uploaded file into an array.
//
$RTFile = file($userfile);
$fileLength = count($RTFile);
// Loop through the rest of the array
for($i = 1; $i < $fileLength; $i++) {
/*
** If the line contains "/colortbl" then we found the color table.
** We'll have to split it up into each individual red, green, and blue
** Convert it to hex and then put the red, green, and blue back together.
** Then store each into an array called fcolor.
*/
if(ereg("^/{///colortbl", $RTFile[$i])) {
// Split the line by the backslash.
$colors = explode("//", $RTFile[$i]);
$numOfColors = count($colors);
for($k = 2; $k < $numOfColors; $k++) {
// Find out how many different colors th
// ere are.
if(ereg("[0-9]+", $colors[$k], $matches)) {
$match[] = $matches[0];
}
}
// For each color, convert it to hex.
$numOfColors = count($match);
for($k = 0; $k < $numOfColors; $k += 3) {
$red = dechex($match[$k]);
$red = $match[$k] < 16 ? "0$red" : $red;
$green = dechex($match[$k + 1]);
$green = $match[$k +1] < 16 ? "0$green" : $green;
$blue = dechex($match[$k + 2]);
$blue = $match[$k + 2] < 16 ? "0$blue" : $blue;
$fcolor[] = "$red$green$blue";
}
$numOfColors = count($fcolor);
}
// Or else, we parse the line, pulling o
// ff words and tags.
else {
$token = "";
$start = 0;
$lineLength = strlen($RTFile[$i]);
for($k = 0; $k < $lineLength; $k++) {
if($RTFile[$i][$start] == "//" && $RTFile[$i][$start + 1] != "//") {
// We are now dealing with a tag.
$token .= $RTFile[$i][$k];
if($RTFile[$i][$k] == " ") {
$newFile[$i] .= ProcessTags($token, $i);
$token = "";
$start = $k + 1;
}
elseif($RTFile[$i][$k] == "/n") {
$newFile[$i] .= ProcessTags($token, $i);
$token = "";
}
}
elseif($RTFile[$i][$start] == "{") {
// We are now dealing with a tag.
$token .= $RTFile[$i][$k];
if($RTFile[$i][$k] == "}") {
$newFile[$i] .= ProcessTags($token, $i);
$token = "";
$start = $k + 1;
}
}
else {
// We are now dealing with a word.
if($RTFile[$i][$k] == "//" && $RTFile[$i][$k + 1] != "//" && $RTFile[$i][$k - 1] != "//") {
$newFile[$i] .= ProcessWord($token);
$token = $RTFile[$i][$k];
$start = $k;
}
else {
$token .= $RTFile[$i][$k];
}
}
}
}
}
$limit = sizeof($newFile);
for($i = 0; $i < $limit; $i++) {
print("$newFile[$i]/n");
}
?>
</body>
</HTML>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 将RTF格式的文件转成HTML并在网页中显示的代码
- 怎样在报表中显示HTML/RTF内容系列之二-将HTML内容存储为RTF格式
- 将PDF转成HTML网页格式的方法
- 将图片转成base64字符串并在JSP页面显示的Java代码
- 在网页中显示带有标签的HTML代码
- 网页(html)中直接显示xml原始文件,直接显示xml格式的文件
- 将RTF文件转换为HTML的方法(借助Office2003)
- Java创建RTF格式的WORD文件代码示例
- 如何在CRichEditCtrl控件中直接读如RTF格式的文件
- [实用小代码]将固定格式的EXCEL版题库转成HTML
- angularJS中把html格式的字符串转成html格式显示,ng-bind-html与trustAsHtml的过滤器
- java连接Postger数据库,并通过Jsp将数据库中数据表的在网页中显示
- ASP.net 从数据库中读取html格式的数据并显示在页面中
- [原创]从RTF格式 的文件中提取文本。
- 将网页转成html
- 网页中显示xml,直接显示xml格式的文件
- 如何将服务器传来的字符串转成HTML显示在前端页面
- html格式代码转换为code格式能在html内显示的代码转换工具
- 为php4加入动态flash文件的生成的支持
- C#代表元及事件触发(Love.NET原创)
- 树型结构列出指定目录里所有文件的PHP类
- Multiple Implementation in C#
- IIS5 HTTP500内部错误解决办法(转自eNet)-------(三)
- 将RTF格式的文件转成HTML并在网页中显示的代码
- 获得存储过程返回值的方法(return的值)
- 利用 window_onload 实现select默认选择
- 自己用的一个求字符串长度的东东,大家可以编译了以后直接引用。
- 如何删除多级目录
- 关于如何添加一个自增的列【原创】
- 用PHP实现多级树型菜单
- C#慢慢学 (一)(e文转)
- 生成sessionid和随机密码的例子