Welcome Guest, Not a member yet? Register   Sign In
I know there is a better way...
#1

[eluser]bcorcoran[/eluser]
What I'm trying to do is parse an HTML table, turning it into a CSV document. (each <tr> gets it's own line, each <td> value is separated by a ",")

I'm trying to turn:
Code:
<table>
  <tr>
    <td>Name1</td>
    <td>Number1</td>
    <td>Instructions1</td>
  </tr>
  <tr>
    <td>Name2</td>
    <td>Number2</td>
    <td>Instructions2</td>
  </tr>
  <tr>
    <td>Name3</td>
    <td>Number3</td>
    <td>Instructions3</td>
  </tr>
</table>
Into this:
Code:
Name1,Number1,Number1
Name2,Number2,Number2
Name3,Number3,Number3

The PHP I have written is for a specific format, but I want a more general parser, so that I can just use this form quickly in the future with any other table no matter what the dimensions are.

Here is what I have (please, save coding practices preachyness for later):

Code:
&lt;?php

if(isset($_POST['data'])) {
    
    $data = $_POST['data'];
    
    $tables = preg_split("/<table.*?&gt;/i",$data);

    $rows = preg_split("/<tr.*?&gt;/i",$tables[1]);
    
    for($x=0;$x<count($rows);$x++) {
        $cells[$x] = preg_split("/<\/td.*?&gt;/i",$rows[$x]);
    }
    
    
    $s = array("\n","\r");
    $r = array("","");
    
    echo "<pre>\n";
    for($i=1;$i<count($cells);$i++) {
        
        $line = array($cells[$i][0],$cells[$i][1],str_replace(", ","\, ",$cells[$i][3]));
        $subject = strip_tags(implode(",",$line));
        echo str_replace($s,$r,$subject) . "\n";
        
    }
    echo "\n</pre>";
    
} else {
    echo "This form takes tabular (HTML Tables) data and converts it to CSV format.";
    echo "&lt;form method=\"post\"&gt;\n";
    echo "&lt;textarea name=\"data\" cols=100 rows=15&gt;&lt;/textarea&gt;\n";
    echo "<br />&lt;input type=\"submit\" value=\"Convert\" /&gt;\n";
    echo "&lt;/form&gt;\n";
}

?&gt;

This works for a specific situation (as you can see I manually specified the cells to use in $line). But, like I said, I want this to be more flexible in the respect that the table could be any dimension.

I know I will figure this out eventually, but I've been thinking myself into a corner for a few hours and I figure maybe someone can smack some sense into me.

Thanks in advance!


Messages In This Thread
I know there is a better way... - by El Forum - 07-11-2007, 01:09 AM
I know there is a better way... - by El Forum - 07-11-2007, 05:57 AM
I know there is a better way... - by El Forum - 07-11-2007, 08:11 AM
I know there is a better way... - by El Forum - 07-11-2007, 08:23 AM
I know there is a better way... - by El Forum - 07-11-2007, 08:32 AM
I know there is a better way... - by El Forum - 07-11-2007, 08:37 AM



Theme © iAndrew 2016 - Forum software by © MyBB