Code _php tutorial for turning RTF files into HTML and displaying them in a Web page

Source: Internet
Author: User
Tags ereg
Code that converts RTF files to HTML and displays them in a Web page
It works like this, upload an RTF file, and then turn it into HTML to show it, the code is a bit complicated, Teaman still have to study, as if the Chinese version is a bit problematic.



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 <=) {$size = 1;}
ElseIf ($size <=) {$size = 2;}
ElseIf ($size <=) {$size = 3;}
ElseIf ($size <=) {$size = 4;}
ElseIf ($size <=) {$size = 5;}
ElseIf ($size <=) {$size = 6;}
ElseIf ($size <=) {$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 to 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 is.
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 is 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 is 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");
}
?>




http://www.bkjia.com/PHPjc/316327.html www.bkjia.com true http://www.bkjia.com/PHPjc/316327.html techarticle Convert RTF files to HTML and display the code in the Web page it works like this, upload an RTF file, and then turn it into HTML to show it, the code is a bit complicated, and the Teaman has to be well developed ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.