Convert an RTF file to HTML and display the code on the webpage.

Source: Internet
Author: User
Tags ereg

Convert an RTF file to HTML and display Code
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>

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.