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 the code displayed on the webpage. convert the 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.



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 \ \
$ 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 inpidual 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 ");
}
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.