將RTF格式的檔案轉成HTML並在網頁中顯示的代碼_PHP

來源:互聯網
上載者:User
關鍵字 顯示 代碼 網頁 格式 檔案 tags size i
將RTF格式的檔案轉成HTML並在網頁中顯示的代碼
它是這樣工作的,將一個RTF檔案上傳,然後轉成HTML顯示出來,代碼有點複雜,teaman還要好好研究,好象中文版有點問題。



if(!($userfile)) {
?>



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 .= "
  • ";
    $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 .= "
  • ";
    $tags = ereg_replace("\\\par", "", $tags);
    $tags = ereg_replace("\\\(tab)", "", $tags);
    $bullets++;
    }
    else {
    $html .= "
";
$bullets = 0;
}
}
// Convert Bold.
if(ereg("\\\b0", $tags)){ $html .= ""; }
elseif(ereg("\\\b", $tags)) { $html .= " "; }
// Convert Italic.
if(ereg("\\\i0", $tags)){ $html .= ""; }
elseif(ereg("\\\i", $tags)) { $html .= ""; }
// Convert Underline.
if(ereg("\\\ulnone", $tags)){ $html .= ""; }
elseif(ereg("\\\ul", $tags)){ $html .= ""; }
// Convert Alignments.
if(ereg("\\\pard\\\qc", $tags)) { $html .= ""; }
elseif(ereg("\\\pard\\\qr", $tags)) { $html .= ""; }
elseif(ereg("\\\pard", $tags)){ $html .= ""; }
// 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 .= "
"; }
// 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 .= "";
}
// 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");
}
?>

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.