将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 相关文章推荐
PHP4在Windows2000下的安装
Oct 09 PHP
据说是雅虎的一份PHP面试题附答案
Jan 07 PHP
php 静态变量的初始化
Nov 15 PHP
通过php快速统计某个数据库中每张表的数据量
Sep 04 PHP
PHP 文件系统详解
Sep 13 PHP
php实现信用卡校验位算法THE LUHN MOD-10示例
May 07 PHP
ECMall支持SSL连接邮件服务器的配置方法详解
May 19 PHP
PHP执行Curl时报错提示CURL ERROR: Recv failure: Connection reset by peer的解决方法
Jun 26 PHP
smarty高级特性之对象的使用方法
Dec 25 PHP
php版微信公众平台接口开发之智能回复开发教程
Sep 22 PHP
php表单文件iframe异步上传实例讲解
Jul 26 PHP
CentOS7.0下安装PHP5.6.30服务的教程详解
Sep 29 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
PHP+MySQL 手工注入语句大全 推荐
2009/10/30 PHP
非常好用的两个PHP函数 serialize()和unserialize()
2012/02/04 PHP
PHP中“简单工厂模式”实例代码讲解
2012/09/04 PHP
30个php操作redis常用方法代码例子
2014/07/05 PHP
学习php设计模式 php实现模板方法模式
2015/12/08 PHP
Zend Framework教程之资源(Resources)用法实例详解
2016/03/14 PHP
PHP中十六进制颜色与RGB颜色值互转的方法
2019/03/18 PHP
javascript中的对象和数组的应用技巧
2007/01/07 Javascript
根据判断浏览器类型屏幕分辨率自动调用不同CSS的代码
2007/02/22 Javascript
javascript 必知必会之closure
2009/09/21 Javascript
JavaScript 原型继承之构造函数继承
2011/08/26 Javascript
js动态添加onload、onresize、onscroll事件(另类方法)
2012/12/26 Javascript
关于javascript event flow 的一个bug详解
2013/09/17 Javascript
JQuery 给元素绑定click事件多次执行的解决方法
2014/09/09 Javascript
实用框架(iframe)操作代码
2014/10/23 Javascript
JavaScript中使用Math.floor()方法对数字取整
2015/06/15 Javascript
基于JavaScript实现添加到购物车效果附源码下载
2016/08/22 Javascript
jQuery源码分析之init的详细介绍
2017/02/13 Javascript
vue使用exif获取图片经纬度的示例代码
2020/12/11 Vue.js
JSON Web Tokens的实现原理
2017/04/02 Python
django 解决manage.py migrate无效的问题
2018/05/27 Python
python 多线程对post请求服务器测试并发的方法
2019/06/13 Python
解决.ui文件生成的.py文件运行不出现界面的方法
2019/06/19 Python
django 快速启动数据库客户端程序的方法示例
2019/08/16 Python
python 如何停止一个死循环的线程
2020/11/24 Python
猎人靴英国官网:Hunter Boots
2017/02/02 全球购物
以特惠价提供在线奢侈品购物:FRMODA.com
2018/01/25 全球购物
优秀英语专业毕业生求职信
2013/11/23 职场文书
英语国培研修感言
2014/02/13 职场文书
园林专业毕业生自荐信
2014/07/04 职场文书
酒店管理专业毕业生自我鉴定
2014/09/29 职场文书
人事任命通知
2015/04/20 职场文书
入党函调证明材料
2015/06/19 职场文书
golang在GRPC中设置client的超时时间
2021/04/27 Golang
Pytorch数据读取之Dataset和DataLoader知识总结
2021/05/23 Python
vue项目中的支付功能实现(微信支付和支付宝支付)
2022/02/18 Vue.js