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


Posted in PHP onOctober 09, 2006

将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>

                 
  

PHP 相关文章推荐
一个简易需要注册的留言版程序
Oct 09 PHP
基于PHP一些十分严重的缺陷详解
Jun 03 PHP
php面向对象中static静态属性和静态方法的调用
Feb 08 PHP
php版微信公众平台接口开发之智能回复开发教程
Sep 22 PHP
php版微信开发Token验证失败或请求URL超时问题的解决方法
Sep 23 PHP
PHP Mysqli 常用代码集合
Nov 12 PHP
php 如何设置一个严格控制过期时间的session
May 05 PHP
解决form中action属性后面?传递参数 获取不到的问题
Jul 21 PHP
php微信支付之公众号支付功能
May 30 PHP
PHP PDOStatement::bindValue讲解
Jan 30 PHP
thinkphp诸多限制条件下如何getshell详解
Dec 09 PHP
PHP使用非对称加密算法RSA
Apr 21 PHP
简单的用PHP编写的导航条程序
Oct 09 #PHP
信用卡效验程序
Oct 09 #PHP
用文本文件实现的动态实时发布新闻的程序
Oct 09 #PHP
构建简单的Webmail系统
Oct 09 #PHP
如何删除多级目录
Oct 09 #PHP
用PHP实现多级树型菜单
Oct 09 #PHP
PHP4在Windows2000下的安装
Oct 09 #PHP
You might like
解析posix与perl标准的正则表达式区别
2013/06/17 PHP
PHP中preg_match函数正则匹配的字符串长度问题
2015/05/27 PHP
PHP实现对数字分隔加千分号的方法
2019/03/18 PHP
如何在Laravel5.8中正确地应用Repository设计模式
2019/11/26 PHP
用jquery ajax获取网站Alexa排名的代码
2009/12/12 Javascript
一个基于jquery的文本框记数器
2012/09/19 Javascript
iframe的父子窗口之间的对象相互调用基本用法
2013/09/03 Javascript
js实现按Ctrl+Enter发送效果
2014/09/18 Javascript
IE6 hack for js 集锦
2014/09/23 Javascript
C#中使用迭代器处理等待任务
2015/07/13 Javascript
用JavaScript获取页面文档内容的实现代码
2016/06/10 Javascript
Vue.js每天必学之数据双向绑定
2016/09/05 Javascript
easyui combogrid实现本地模糊搜索过滤多列
2017/05/13 Javascript
AngularJS 的$timeout服务示例代码
2017/09/21 Javascript
Vue 中mixin 的用法详解
2018/04/23 Javascript
详解JavaScript添加给定的标签选项
2018/09/17 Javascript
微信小程序时间轴实现方法示例
2019/01/14 Javascript
vue实现路由懒加载及组件懒加载的方式
2019/06/11 Javascript
vue+element导航栏高亮显示的解决方式
2019/11/12 Javascript
在微信小程序中使用mqtt服务的方法
2019/12/13 Javascript
给Python初学者的一些编程技巧
2015/04/03 Python
Python决策树之基于信息增益的特征选择示例
2018/06/25 Python
利用anaconda保证64位和32位的python共存
2021/03/09 Python
django2笔记之路由path语法的实现
2019/07/17 Python
python分割一个文本为多个文本的方法
2019/07/22 Python
Pycharm小白级简单使用教程
2020/01/08 Python
python使用hdfs3模块对hdfs进行操作详解
2020/06/06 Python
详解rem 适配布局
2018/10/31 HTML / CSS
大学生求职简历的自我评价范文
2013/10/12 职场文书
廉政教育心得体会
2014/01/01 职场文书
三八节主持词
2014/03/17 职场文书
2014年教师节座谈会发言稿
2014/09/10 职场文书
刑事代理授权委托书
2014/09/17 职场文书
地下停车场租赁协议范本
2014/10/07 职场文书
期末复习计划
2015/01/19 职场文书
Python包管理工具pip的15 个使用小技巧
2021/05/17 Python