Home Backend Development PHP Tutorial Code to convert RTF format files into HTML and display them on the web page_PHP tutorial

Code to convert RTF format files into HTML and display them on the web page_PHP tutorial

Jul 21, 2016 pm 04:03 PM
html rtf code Will document show Format of Web page

Code for converting RTF format files into HTML and displaying them on the web page
It works like this, upload an RTF file, and then convert it into HTML and display it. The code is a bit complicated, and teaman needs to study it carefully. It seems like Chinese There is something wrong with the version.

    
    
        if(!($userfile)) {
    ?>
    


    
    Send this file:
    
    

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

                 
  

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/316327.htmlTechArticle将RTF格式的文件转成HTML并在网页中显示的代码 它是这样工作的,将一个RTF文件上传,然后转成HTML显示出来,代码有点复杂,teaman还要好好研...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles