Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8101765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:04:02+00:00 2026-06-05T23:04:02+00:00

I’m working on a project that requires to convert html email into text. Below

  • 0

I’m working on a project that requires to convert html email into text. Below is a simplified version of the HTML code:

<table>
    <tr>
        <td width="10%"></td>
        <td width="60%"> test product </td>
        <td width="20%">5</td>
        <td width="10%"> £50.00 </td>
    </tr>
    <tr>
        <td></td>
        <td colspan="3" width="100%"> Project Name: Test Project </td>
    </tr>
    <tr>
        <td width="10%"> </td>
        <td colspan="2" width="80%"> Page 1 : 01 New York 1.jpg </td>
        <td width="10%"> £0.00 </td>
    </tr>
</table>

The expected outcome should look like this in a text file (with columns aligned nicely):

test product                                      5            £50.00
Project Name: Test Project                                                            
Page 1 :  01 New York 1.jpg                                    £0.00

My idea is parsing the HTML content by DOMDocument. Then I will set a default width for the table (i.e.: 100 spaces) then convert the width of each column from % to number of spaces (based on colspan & width attribute of <td> tag). Then I will subtract these column width to strlen of the data in each column to archive the number of spaces I need to pad_right to the string to make everything align vertically.

I have been working that way, hasn’t been archived what I want but just wondering if it is stupid or anyone knows a better way please help me out.

Also when it comes to Multibyte languages (Japanese, Korean etc…) I don’t think my approach would work because their characters will be bigger than one space and it end up a mess.

Can someone help me out please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:04:04+00:00Added an answer on June 5, 2026 at 11:04 pm

    Don’t reinvent the wheel. Table rendering is difficult, rendering tables using only text is even more difficult.
    To clarify the complexity of a text-based table renderer that offers all the features of HTML, take a look at w3m, which is open source:
    these 3000 lines of code are there only to display html tables.

    Transform HTML to Text

    There are textbased browsers that can be used by command line, like lynx.
    You could fwrite your html table into a file, pass that file into the textbased browser and take its output.

    Note: textbased browsers are generally used in a shell, which generally displays in monospace. This remains a prerequisite.

    lynx and w3m are both available on Windows and you don’t need to “install” them, you just need to have the executables and the permission to run them from PHP.

    code example:

    <?php
    $table = '<table><tr><td>foo</td><td>bar</td></tr></table>'; //this contains your table
    $html = "<html><body>$table</body></html>";
    
    //write html file
    $tmpfname = tempnam(sys_get_temp_dir(), "tblemail");
    
    $handle = fopen($tmpfname, "w");
    fwrite($handle, $html);
    fclose($handle);
    
    $myTextTable = shell_exec("w3m.exe -dump \"$tmpfname\"");
    unlink($tmpfname);
    

    w3m.exe needs to be in your working directory.

    (didn’t try it)

    Render a Text table

    If you want a native PHP solution, there’s also at least one framework (https://github.com/c9s/CLIFramework) aimed at console applications for PHP which has a table renderer.

    It doesn’t transform HTML to text, but it helps you build a text formatted table with support for multiline cells (which seems to be the most complicated part).

    Using CLIFramework you would need a code like this to render your table:

    <?php
    require 'vendor/autoload.php';
    use CLIFramework\Component\Table\Table;
    
    $table = new Table;
    $table->addRow(array( 
        "test product", "5", "£50.00"
    ));
    $table->addRow(array( 
        "Project Name: Test Project", "", ""
    ));
    $table->addRow(array( 
        "Page 1 : 01 New York 1.jpg", "", "£0.00"
    ));
    
    $myTextTable = $table->render();
    

    The CLIFramework table renderer doesn’t seem to support anything similar to “colspan” however.

    Here’s the documentation for the table component: https://github.com/c9s/CLIFramework/wiki/Using-Table-Component

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have two tables with like below codes: Table: Accounts id | username |
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.