将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 相关文章推荐
php下使用iconv需要注意的问题
Nov 20 PHP
PHP实现单例模式最安全的做法
Jun 13 PHP
destoon安装出现Internal Server Error的解决方法
Jun 21 PHP
Thinkphp实现MySQL读写分离操作示例
Jun 25 PHP
浅析iis7.5安装配置php环境
May 10 PHP
php实现的Curl封装类Curl.class.php用法实例分析
Sep 25 PHP
php支付宝在线支付接口开发教程
Sep 19 PHP
Yii2实现多域名跨域同步登录退出
Feb 04 PHP
详解thinkphp中的volist标签
Jan 15 PHP
PHP5.6.8连接SQL Server 2008 R2数据库常用技巧分析总结
May 06 PHP
PHP面向对象程序设计__tostring()和__invoke()用法分析
Jun 12 PHP
thinkphp5 框架结合plupload实现图片批量上传功能示例
Apr 04 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中的常用函数回顾
2013/07/11 PHP
PHP5多态性与动态绑定介绍
2015/04/03 PHP
php中的登陆login实例代码
2016/06/20 PHP
重定向实现代码
2006/11/20 Javascript
jQuery 创建Dom元素
2010/05/07 Javascript
Dom与浏览器兼容性说明
2010/10/25 Javascript
使用jQuery快速解决input中placeholder值在ie中无法支持的问题
2014/01/02 Javascript
nodejs下打包模块archiver详解
2014/12/03 NodeJs
Angularjs实现mvvm式的选项卡示例代码
2016/09/08 Javascript
Ionic3 UI组件之Gallery Modal详解
2017/06/07 Javascript
初探JavaScript 面向对象(推荐)
2017/09/03 Javascript
详解react内联样式使用webpack将px转rem
2018/09/13 Javascript
Vue-CLI3.x 设置反向代理的方法
2018/12/06 Javascript
vue移动端屏幕适配详解
2019/04/30 Javascript
vue路由拦截器和请求拦截器知识点总结
2019/11/08 Javascript
Vue axios获取token临时令牌封装案例
2020/09/11 Javascript
JS+JQuery实现无缝连接轮播图
2020/12/30 jQuery
利用 Chrome Dev Tools 进行页面性能分析的步骤说明(前端性能优化)
2021/02/24 Javascript
Python中使用OpenCV库来进行简单的气象学遥感影像计算
2016/02/19 Python
使用Python的Flask框架表单插件Flask-WTF实现Web登录验证
2016/07/12 Python
利用Tkinter和matplotlib两种方式画饼状图的实例
2017/11/06 Python
Python实现读写INI配置文件的方法示例
2018/06/09 Python
Python读取txt文件数据的方法(用于接口自动化参数化数据)
2018/06/27 Python
在python中pandas的series合并方法
2018/11/12 Python
Django框架视图层URL映射与反向解析实例分析
2019/07/29 Python
python实现通过队列完成进程间的多任务功能示例
2019/10/28 Python
Python面向对象之继承原理与用法案例分析
2019/12/31 Python
下载与当前Chrome对应的chromedriver.exe(用于python+selenium)
2020/01/14 Python
tensorflow通过模型文件,使用tensorboard查看其模型图Graph方式
2020/01/23 Python
python 连续不等式语法糖实例
2020/04/15 Python
新东网科技Java笔试题
2012/07/13 面试题
一个SQL面试题
2014/08/21 面试题
北京-环亚运商测试题.net程序员初步测试题
2013/05/28 面试题
北京麒麟网信息技术有限公司网络游戏测试面试题
2013/09/28 面试题
老教师工作总结的自我评价
2013/09/27 职场文书
培训心得体会怎么写
2016/01/25 职场文书