Convert an RTF file to HTML and display the code on the webpage.
This is how it works. It uploads an RTF file and converts it to HTML for display. The code is a bit complicated and teaman needs to study it well, as if the Chinese version is a bit problematic.
<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 \ \
$ Word = ereg_replace ("[\] {2,}", "\", $ word );
// Replace \ {{
$ Word = ereg_replace ("[\] [\ {]", "\ {", $ word );
// Replace \}}
$ 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>