views:

206

answers:

6

Not sure if this is an original idea but it seems like a fun idea either way.

Lets see who can convert something like this...

<table>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

To this...

+-----+-----+-----+
|     |     |     |
|     |     |     |
+-----+-----+-----+
|     |     |     |
|     |     |     |
+-----+-----+-----+
|     |     |     |
|     |     |     |
+-----+-----+-----+

Ive been thinking about it but my head can't get round it.

A: 

Hi! maybe you should try regular expressions or xml parser and http://framework.zend.com/manual/en/zend.text.table.introduction.html

valya
+2  A: 

First you analyze the html to see how many rows and columns you need (run regex or loop: find number of td's in any tr (assume all same number of colums) then find all trs in table). then you determine the height and width you want. You're going to have a string, start it with a +, then add the number of -'s you need for the width, with another + at the end. Repeat for number of columns, add newline at the end.

Use the same idea to do the same thing using | and space characters.

Print the +- line, print the other line the number of times you need to for the height. Save as a string. Print that string the number of columns you need. end with a + line.

That's just pseudo code, I assume the language would be javascript. That's my take on it at least. ;D

CrazyJugglerDrummer
I had PHP in mind, simply because thats the code I'm most comfortable with.
Ben Shelock
+2  A: 

This works. C# on an .aspx page - might need to play with the number of "& nbsp ;". Yes, some best practices were sacrificed in exchange for coding speed.

string tbl = "<table>  <tr>    <td></td>    <td></td>    <td></td>  </tr>  <tr>    <td></td>    <td></td>    <td></td>  </tr>  <tr>    <td></td>    <td></td>    <td></td>  </tr></table>";
int rows = tbl.Length - tbl.Replace("<tr>", "123").Length;
int cols = (tbl.Length - tbl.Replace("<td>", "123").Length) / rows;
StringBuilder sb = new StringBuilder();

sb.Append("+");

for (int col = 0; col < cols; col++)
         sb.Append("-----+");

sb.Append("<br />");


for (int row = 0; row < rows; row++)
{
    for (int repeat = 0; repeat < 2; repeat++)
    {
        sb.Append("&nbsp;|");

        for (int col = 0; col < cols; col++)
            sb.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|");

        sb.Append("<br />");
    }

    sb.Append("+");

    for (int col = 0; col < cols; col++)
        sb.Append("-----+");

    sb.Append("<br />");
}

Response.Write(sb.ToString());
JBrooks
A: 

Hey mate, here's a solution with PHP

function TableToText( $table_data )
{
 $table_data = trim( $table_data );

 if( preg_match( "/<table>(.*)<\/table>/si", $table_data, $m ) )
 {
  $table = $m[1];

  if( preg_match_all( "/(?:<tr>(.*?)<\/tr>)+/si", $table, $m ) )
  {
   $row_data = $m[1];
   $max_width = array();

   $rows = array();

   foreach( $row_data as $row )
   {
    preg_match_all( "/(?:<td>(.*?)<\/td>)+/si", $row, $m );

    $rows[] = $m[1];

    for( $a = 0; $a < sizeof( $m[1] ); $a++ ) 
    {
     $max_width[ $a ] = strlen( $m[1][$a] ) > $max_width[ $a ] ? strlen( $m[1][$a] ) : $max_width[ $a ];
    }
   }

   $rowstr = '+';

   foreach( $max_width as $width )
   {
    for( $b = 0; $b < $width; $b ++ )
    {
     $rowstr .= '-';
    }
    $rowstr .= '--+';
   }   

   foreach( $rows as $row )
   {
    $tablestr .= $rowstr . "\n";

    for( $a = 0; $a < sizeof( $max_width ); $a++ )
    {
     $tablestr .= '| ';
     $tablestr .= $row[ $a ];

     for( $b = strlen( $row[ $a ] ); $b < $max_width[ $a ]; $b++ )
     {
      $tablestr .= ' ';
     }

     $tablestr .= ' ';
    }
    $tablestr .= "|\n";
   }

   $tablestr .= $rowstr;
   return $tablestr;
  }
 }
}
Kane Wallmann
+2  A: 

Here's a Perl solution that someone's already made.

Jason Berry
+1  A: 

I think a text-based browser would solve this challenge very nicely.
Check out: lynx or links

instcode