0

Hi

I want to Convert HTML table in to text file using PHP

for that what i have to do?

pls Suggest me

Thanks All,
Ajay

2
Contributors
1
Reply
7
Views
9 Years
Discussion Span
Last Post by digital-ether
0

Hi

I want to Convert HTML table in to text file using PHP

for that what i have to do?

pls Suggest me

Thanks All,
Ajay

Is it just a single table, or nested tables (can there be tables inside that table).

Consider the table:

<table>
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
</tr>
<tr>
<td>Row 2 Data 1</td>
<td>Row 2 Data 2</td>
</tr>
</table>

If your table is well formatted, you could use an XML parser, like DOM or SimpleXML etc.
http://www.php.net/simplexml
http://www.php.net/manual/en/book.domxml.php

However, if it isn't then you can just use Regular Expressions.

I'm going to attempt a basic example for the above table HTML, but it probably won't work as is:

preg_match_all("/<tr>(.*?)<\/tr>/", $table, $matches);
$rows = $matches[1];

Now all the table rows should be in $rows.

We have to go through each table row, and get out each data column.

foreach($rows as $row) {
preg_match_all("/<td>(.*?)<\/td>/", $row, $matches);
$data[] = $matches[1];
}

Now the array $data should hold all the table rows, in a array for each column

You can then iterate through that array and write the results to a text file.

eg:

$file = fopen('file.txt', 'w');
foreach($data as $row) {

$line = implode("," $row)."\n";
fwrite($file, $line, strlen($line));


}
fclose($file);

Notice the delimiters are "," for each data column and "\n" (line break) for each row.

Hope that give you something to start with for your own purposes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.