将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 adodb分页实现代码
Mar 19 PHP
PHP无限分类(树形类)的深入分析
Jun 02 PHP
使用session判断用户登录用户权限(超简单)
Jun 08 PHP
php获取qq用户昵称和在线状态(实例分析)
Oct 27 PHP
PHP命名空间(Namespace)简明教程
Jun 11 PHP
PHP常见的6个错误提示及解决方法
Jul 07 PHP
Yii中的cookie的发送和读取
Jul 27 PHP
PHP7扩展开发之hello word实现方法详解
Jan 15 PHP
PHP面向对象五大原则之里氏替换原则(LSP)详解
Apr 08 PHP
JSON PHP中,Json字符串反序列化成对象/数组的方法
May 31 PHP
PHP删除数组中指定值的元素常用方法实例分析【4种方法】
Aug 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
一个程序下载的管理程序(三)
2006/10/09 PHP
PHP 实现explort() 功能的详解
2013/06/20 PHP
PHP5全版本绕过open_basedir读文件脚本漏洞详细介绍
2015/01/20 PHP
理解 JavaScript 预解析
2009/10/25 Javascript
JQuery 图片的展开和伸缩实例讲解
2013/04/18 Javascript
让jQuery Mobile不显示讨厌loading界面的方法
2014/02/19 Javascript
JavaScript实现下拉菜单的显示和隐藏
2016/01/05 Javascript
JavaScript作用域示例详解
2016/07/07 Javascript
AngularJS ng-bind-template 指令详解
2016/07/30 Javascript
Javascript 创建类并动态添加属性及方法的简单实现
2016/10/20 Javascript
js中开关变量使用实例
2017/02/24 Javascript
Bootstrap媒体对象学习使用
2017/03/07 Javascript
使用Object.defineProperty如何巧妙找到修改某个变量的准确代码位置
2018/11/02 Javascript
微信小程序实现的五星评价功能示例
2019/04/25 Javascript
JS开发 富文本编辑器TinyMCE详解
2019/07/19 Javascript
js动态获取时间的方法分析
2019/08/02 Javascript
微信小程序录音实现功能并上传(使用node解析接收)
2020/02/26 Javascript
vue实现图片按比例缩放问题操作
2020/08/11 Javascript
解决vue页面刷新,数据丢失的问题
2020/11/24 Vue.js
JavaScript 生成唯一ID的几种方式
2021/02/19 Javascript
[30:51]DOTA2上海特级锦标赛主赛事日 - 3 胜者组第二轮#1Liquid VS MVP.Phx第一局
2016/03/04 DOTA
用Python操作字符串之rindex()方法的使用
2015/05/19 Python
python实现微信每日一句自动发送给喜欢的人
2019/04/29 Python
Python获取基金网站网页内容、使用BeautifulSoup库分析html操作示例
2019/06/04 Python
python字符串切割:str.split()与re.split()的对比分析
2019/07/16 Python
Python3 使用map()批量的转换数据类型,如str转float的实现
2019/11/29 Python
通过CSS3的object-fit来调整图片适配尺寸的技巧简介
2016/02/27 HTML / CSS
CSS3让登陆面板3D旋转起来
2016/05/03 HTML / CSS
anello泰国官方网站:日本流行包包品牌
2019/08/08 全球购物
乌克兰第一的珠宝网上商店:Gold.ua
2019/11/29 全球购物
SOKOLOV官网:俄罗斯珠宝首饰品牌
2021/01/02 全球购物
餐饮总经理岗位职责
2014/03/07 职场文书
2014副镇长民主生活会个人对照检查材料思想汇报
2014/09/30 职场文书
综治工作汇报材料
2014/10/27 职场文书
基于Redis zSet实现滑动窗口对短信进行防刷限流的问题
2022/02/12 Redis
MySQL 逻辑备份 into outfile
2022/05/15 MySQL