FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


converting <TABLE> to CSV





MrBaseball34
I need a PHP script where I can pass the outerHTML of a <Table> tag
and it will return the table data in CSV format.
MrBaseball34
Bumping this so maybe new people will see it.. Wink
goansfr
I try it :

Use microsoft excel. Click right on the table in your webbrowser (IE only works). You have "export to excel". then u have just to save your excel file in .csv
MrBaseball34
I want to do it programatically using PHP.
hexkid
MrBaseball34 wrote:
I need a PHP script where I can pass the outerHTML of a <Table> tag and it will return the table data in CSV format.


What happens when you have <table>s inside the <table> you pass to PHP?

If the only tags you're passing to PHP are <table>, </table> (once each only), <tr>, </tr>, <td>, </td> without any attributes a "simple" regular expression parser might do it.

ATTENTION: <table class="ab>cd">...</table> is valid html!

Code:
<?php
$input = '<table>
  <tr>
    <td>one</td>
    <td>two</td>
  </tr>
  <tr>
    <td>three</td>
    <td>four</td>
  </tr>
</table>';

echo '<pre>';
preg_match_all('/<tr>(.*)<\/tr>/isU', $input, $trs);
foreach ($trs[1] as $tr) {
  preg_match_all('/<td>(.*)<\/td>/isU', $tr, $tds);
  $first = true;
  foreach ($tds[1] as $td) {
    if (!$first) echo ', ';
    echo '"', $td, '"';
    $first = false;
  }
  echo "\n";
}
echo '</pre>';
?>
Related topics
Validation problems with <form>
html tag <table help?
Styling <abbr> in IE
<hr> stilosi con i CSS
Adding <br /> to text area
Is it possible to change the bgcolor of <iframe> in IE
Using <DIV> and <TABLE> both in a layout
Table Cell Widths in HTML
Force Javascript to write into <body> ??
Force javascript to write codes into <body> ?
hot to make flahing table
PhpBB Ranks <RESOLVED>
Congrats Tony - the power of <title> and <meta desc
from excel to php ?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.