热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将RTF格式的文件转成HTML并在网页中显示的代码

将RTF格式的文件转成HTML并在网页中显示的代码它是这样工作的,将一个RTF文件上传,然后转成HTML显示出来,代码有点复杂,teaman还要好好研究,好象中文版有点问题。htmlbody?if(!($userfile)){?formenctypemultipartform-dataaction?print($PHP_SELF);?me
将RTF格式的文件转成HTML并在网页中显示的代码
它是这样工作的,将一个RTF文件上传,然后转成HTML显示出来,代码有点复杂,teaman还要好好研究,好象中文版有点问题。



if(!($userfile)) {
?>
" method=post>

Send this file:




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 .= "
  • ";
    $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 .= "
  • ";
    $tags = ereg_replace("\\\par", "", $tags);
    $tags = ereg_replace("\\\(tab)", "", $tags);
    $bullets++;
    }
    else {
    $html .= "
";
$bullets = 0;
}
}
// Convert Bold.
if(ereg("\\\b0", $tags)){ $html .= ""; }
elseif(ereg("\\\b", $tags)) { $html .= ""; }
// Convert Italic.
if(ereg("\\\i0", $tags)){ $html .= ""; }
elseif(ereg("\\\i", $tags)) { $html .= ""; }
// Convert Underline.
if(ereg("\\\ulnone", $tags)){ $html .= ""; }
elseif(ereg("\\\ul", $tags)){ $html .= ""; }
// Convert Alignments.
if(ereg("\\\pard\\\qc", $tags)) { $html .= "

"; }
elseif(ereg("\\\pard\\\qr", $tags)) { $html .= "

"; }
elseif(ereg("\\\pard", $tags)){ $html .= "

"; }
// 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 .= "
"; }
// 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 .= "";
}
// 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 inpidual 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");
}
?>


推荐阅读
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 本文描述了作者第一次参加比赛的经历和感受。作者是小学六年级时参加比赛的唯一选手,感到有些紧张。在比赛期间,作者与学长学姐一起用餐,在比赛题目中遇到了一些困难,但最终成功解决。作者还尝试了一款游戏,在回程的路上感到晕车。最终,作者以110分的成绩取得了省一会的资格,并坚定了继续学习的决心。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 关羽败走麦城时路过马超封地 马超为何没有出手救人
    对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
author-avatar
大坑啊同志
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有