[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:
<?php
if(isset($_POST['data'])) {
$data = $_POST['data'];
$tables = preg_split("/<table.*?>/i",$data);
$rows = preg_split("/<tr.*?>/i",$tables[1]);
for($x=0;$x<count($rows);$x++) {
$cells[$x] = preg_split("/<\/td.*?>/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 "<form method=\"post\">\n";
echo "<textarea name=\"data\" cols=100 rows=15></textarea>\n";
echo "<br /><input type=\"submit\" value=\"Convert\" />\n";
echo "</form>\n";
}
?>
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!