将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 相关文章推荐
Ajax+PHP边学边练 之五 图片处理
Dec 03 PHP
PHP 编程安全性小结
Jan 08 PHP
基于Linux调试工具strace与gdb的常用命令总结
Jun 03 PHP
实例介绍PHP的Reflection反射机制
Aug 05 PHP
php中HTTP_REFERER函数用法实例
Nov 21 PHP
phpmyadmin提示The mbstring extension is missing的解决方法
Dec 17 PHP
PHP+APACHE实现网址伪静态
Feb 22 PHP
php将html转成wml的WAP标记语言实例
Jul 08 PHP
CodeIgniter视图使用注意事项
Jan 20 PHP
CI框架的安全性分析
May 18 PHP
thinkphp3.x中变量的获取和过滤方法详解
May 20 PHP
Laravel使用原生sql语句并调用的方法
Oct 09 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
thinkphp 多表 事务详解
2013/06/17 PHP
Laravel 模型使用软删除-左连接查询-表起别名示例
2019/10/24 PHP
提高网站性能之 如何对待JavaScript
2009/10/31 Javascript
基于jQuery的让非HTML5浏览器支持placeholder属性的代码
2011/05/24 Javascript
深入理解javaScript中的事件驱动
2013/05/21 Javascript
AngularJS实现全选反选功能
2015/12/08 Javascript
JS简单实现获取元素的封装操作示例
2017/04/07 Javascript
使用 NodeJS+Express 开发服务端的简单介绍
2017/04/07 NodeJs
使用原生js写ajax实例(推荐)
2017/05/31 Javascript
Node 自动化部署的方法
2017/10/17 Javascript
Node.js学习教程之HTTP/2服务器推送【译】
2017/10/31 Javascript
基于node搭建服务器,写接口,调接口,跨域的实例
2018/05/13 Javascript
Layui动态生成select下拉选择框不显示的解决方法
2019/09/24 Javascript
解决layui页面按钮点击无反应,也不报错的问题
2019/09/29 Javascript
超简单的微信小程序轮播图
2019/11/22 Javascript
vue实现购物车案例
2020/05/30 Javascript
DWR内存兼容及无法调用问题解决方案
2020/10/16 Javascript
简介Django框架中可使用的各类缓存
2015/07/23 Python
python使用Tesseract库识别验证
2018/03/21 Python
python 对象和json互相转换方法
2018/03/22 Python
浅谈Python中的bs4基础
2018/10/21 Python
pandas 透视表中文字段排序方法
2018/11/16 Python
详解从Django Rest Framework响应中删除空字段
2019/01/11 Python
python整小时 整天时间戳获取算法示例
2019/02/20 Python
简单了解python反射机制的一些知识
2019/07/13 Python
Python基础教程(一)——Windows搭建开发Python开发环境
2020/07/20 Python
python openCV实现摄像头获取人脸图片
2020/08/20 Python
用Python自动清理电脑内重复文件,只要10行代码(自动脚本)
2021/01/09 Python
产品生产计划书
2014/05/07 职场文书
创先争优一句话承诺
2014/05/29 职场文书
党委班子剖析材料
2014/08/21 职场文书
2014领导班子“四风问题”对照检查材料思想汇报(执法局)
2014/09/21 职场文书
2014红色之旅心得体会
2014/10/07 职场文书
Vue项目中如何封装axios(统一管理http请求)
2021/05/02 Vue.js
python使用glob检索文件的操作
2021/05/20 Python
java如何实现socket连接方法封装
2021/09/25 Java/Android