欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-15 08:16:47
将rtf格式的文件转成html并在网页中显示的代码 它是这样工作的,将一个rtf文件上传,然后转成html显示出来,代码有点复杂,teaman还要好好研究,好象中文版有点问...
将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>