使用PHP自动将HTML表格转换为CSV?

I am just in need to convert a this html table automatically in csv using PHP. Can someone provide any idea how to do this? Thanks.

$table = '<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>';

Guys, I just need $table to convert in only .csv file, which could be automatically generated using some PHP function. We can define path for that csv file to /test/home/path_to_csv

dssjxvbv918586
dssjxvbv918586 我收到一个错误,我的csv是空白的.....我有一个文件,其中有标题标签和23个更多的标签与表现在如何我将阅读它并解析它。$file=file_get_contents($file_txt。“/D_LOTMAN.HTM”);的print_r($文件);包括'./simple_html_dom.php';$html=str_get_html($file);$fpc=fopen(“downloads/me.csv”,“w”);foreach($html->find('tr')as$element){$td=array();foreach($element->find('td')as$row){$td[]=$row->plaintext;}fputcsv($fpc,$td);}fclose($fpc);
6 年多之前 回复
dongliao1860
dongliao1860 你试过这个吗?davidvielmetter.com/tricks/...
8 年多之前 回复
doue1925
doue1925 为了从HTML字符串中获取结构,您需要解析数据(在这种情况下存储在变量中)。然后,您可以提取您感兴趣的信息并将其存储为CSV格式
8 年多之前 回复
doujing5435
doujing5435 我不想解析任何东西。我有自己的变量$table,其中包含html表数据。现在我想使用PHP将该数据转换为.csv文件
8 年多之前 回复
duanmi4379
duanmi4379 你到底想要达到什么目的?您是要解析HTML并提取数据还是一次性分析?对于一次性,我会建议在这里找到的信息。对于更通用和自动化的工作,需要解析HTML-尝试简单解析。或者,如果HTML是固定格式的,那么您可以通过正则表达式来抓取标签并仅保留数据。(例如,用一些字段分隔符替换td)然后,CSV输出是一件小事。
8 年多之前 回复
doujing5846
doujing5846 我意识到这不是你想要的,但你可以将其保存为xls。并且excel将按预期打开它。
8 年多之前 回复
doujing4555
doujing4555 我搜索了谷歌,但没有找到相关的
8 年多之前 回复

8个回答

You can use str_get_html http://simplehtmldom.sourceforge.net/

include "simple_html_dom.php";
$table = '<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>';

$html = str_get_html($table);



header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=sample.csv');

$fp = fopen("php://output", "w");

foreach($html->find('tr') as $element)
{
    $td = array();
    foreach( $element->find('th') as $row)  
    {
        $td [] = $row->plaintext;
    }
    fputcsv($fp, $td);

    $td = array();
    foreach( $element->find('td') as $row)  
    {
        $td [] = $row->plaintext;
    }
    fputcsv($fp, $td);
}


fclose($fp);
drwkqwa82140
drwkqwa82140 我尝试了这种方法,csv正在创建,但表列内容不存在于csv中。 相反,内容写入两次。 知道为什么吗? 我使用1.8.1版本
一年多之前 回复
dongzhao3040
dongzhao3040 在lib代码中,MAX_FILE_SIZE设置为600000,如果函数file_get_html和str_get_html获得超过600000的字符串大小,则不允许进展并返回false。 我希望使用一个包含字符串长度高达100万及以上的大表,我该怎么办,你能否告诉我MAX_FILE_SIZE数字的重要性。
大约 2 年之前 回复
dongsetan3216
dongsetan3216 fputcsv具有您可以试验的其他参数,并确保过滤掉空数组
8 年多之前 回复
dongqiao6445
dongqiao6445 嗨,我正在获取CSV输出,如照片i.imgur.com/x2mWb.png,它在数据之间插入额外的空白行。 可以修复吗? 我试过trim()。 但它没有用。
8 年多之前 回复
douliu8327
douliu8327 哎呀! 对不起我的上一条评论。 有效。 再次感谢。
8 年多之前 回复
doubenggua9430
doubenggua9430 哎呀! 巴巴,我将php://输出更改为/ mohan / path_to_csv,当我加载页面时,它会发出几个警告! 如警告:fopen(/ mohan / path_to_csv)[function.fopen]:无法打开流:第29行的C:\ wamp \ www \ mohan \ TableToCsv.php中没有这样的文件或目录。第29行是$ fp = fopen (“/ mohan / path_to_csv”,“w”);
8 年多之前 回复
duanran3115
duanran3115 很酷....自己测试一下:D
8 年多之前 回复
duankan6894
duankan6894 谢谢你的方法。 我会在我的电脑上试试(目前我在移动设备上),让你知道。
8 年多之前 回复
douqianxian7008
douqianxian7008 是的你可以.....你需要做的就是删除标题....并将“php:// output”更改为你的路径
8 年多之前 回复
duanmo7075
duanmo7075 Baba,我们如何为该csv文件定义一些路径以自动保存到例如 /测试/首页/ path_to_csv
8 年多之前 回复

You can do this with arrays and regular expressions... See below

$csv = array();
preg_match('/<table(>| [^>]*>)(.*?)<\/table( |>)/is',$table,$b);
$table = $b[2];
preg_match_all('/<tr(>| [^>]*>)(.*?)<\/tr( |>)/is',$table,$b);
$rows = $b[2];
foreach ($rows as $row) {
    //cycle through each row
    if(preg_match('/<th(>| [^>]*>)(.*?)<\/th( |>)/is',$row)) {
        //match for table headers
        preg_match_all('/<th(>| [^>]*>)(.*?)<\/th( |>)/is',$row,$b);
        $csv[] = strip_tags(implode(',',$b[2]));
    } elseif(preg_match('/<td(>| [^>]*>)(.*?)<\/td( |>)/is',$row)) {
        //match for table cells
        preg_match_all('/<td(>| [^>]*>)(.*?)<\/td( |>)/is',$row,$b);
        $csv[] = strip_tags(implode(',',$b[2]));
    }
}
$csv = implode("
", $csv);
var_dump($csv);

Then you can use file_put_contents() to write the csv string to file..

douchuang4402
douchuang4402 大! 并且在目前的背景下也有效回答。 :-)
大约 2 年之前 回复
doumian3780
doumian3780 我只使用了5年或更久以前提供的范围。 如果你正确地逃避你的字符串,他们应该保持他们的列结构
大约 2 年之前 回复
douji9816
douji9816 经典答案。 如何处理要放入CSV的HTML标记?
大约 2 年之前 回复

You can use this function in separate js file:

function exportTableToCSV($table, filename) {

        var $rows = $table.find('tr:has(td)'),

            // Temporary delimiter characters unlikely to be typed by keyboard
            // This is to avoid accidentally splitting the actual contents
            tmpColDelim = String.fromCharCode(11), // vertical tab character
            tmpRowDelim = String.fromCharCode(0), // null character

            // actual delimiter characters for CSV format
            colDelim = '","',
            rowDelim = '"
"',

            // Grab text from table into CSV formatted string
            csv = '"' + $rows.map(function (i, row) {
                var $row = $(row),
                    $cols = $row.find('td');

                return $cols.map(function (j, col) {
                    var $col = $(col),
                        text = $col.text();

                    return text.replace('"', '""'); // escape double quotes

                }).get().join(tmpColDelim);

            }).get().join(tmpRowDelim)
                .split(tmpRowDelim).join(rowDelim)
                .split(tmpColDelim).join(colDelim) + '"',

            // Data URI
            csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);

        $(this)
            .attr({
            'download': filename,
                'href': csvData,
                'target': '_blank'
        });
    }

Now, to initiate this function, you can use:

$('.getfile').click(
            function() { 
    exportTableToCSV.apply(this, [$('#thetable'), 'filename.csv']);
             });

where 'getfile' should be the class assigned to button, where you want to add call to action. (On clicking this button, the download popup will appear) and "thetable" should be the ID assigned to table you want to download.

You can also change to the custom file name to download in code.

duanmao1975
duanmao1975 这项工作出色。 为了你的荣誉,我将牺牲100头公牛。
接近 5 年之前 回复

To expand on the accepted answer I did this which allows me to ignore columns by class name and also deals with blank rows/columns.

You can use str_get_html http://simplehtmldom.sourceforge.net/. Just include it and away you go! :)

$html = str_get_html($html); // give this your HTML string

header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=sample.csv');

$fp = fopen("php://output", "w");

foreach($html->find('tr') as $element) {
  $td = array();
  foreach( $element->find('th') as $row) {
    if (strpos(trim($row->class), 'actions') === false && strpos(trim($row->class), 'checker') === false) {
      $td [] = $row->plaintext;
    }
  }
  if (!empty($td)) {
    fputcsv($fp, $td);
  }

  $td = array();
  foreach( $element->find('td') as $row) {
    if (strpos(trim($row->class), 'actions') === false && strpos(trim($row->class), 'checker') === false) {
      $td [] = $row->plaintext;
    }
  }
  if (!empty($td)) {
    fputcsv($fp, $td);
  }
}

fclose($fp);
exit;

If anyone is using Baba's answer but scratching their head over extra white spaces being added, this will work:

include "simple_html_dom.php";
$table = '<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>';

$html = str_get_html($table);   

$fileName="export.csv";
header('Content-type: application/ms-excel');
header("Content-Disposition: attachment; filename=$fileName");

$fp = fopen("php://output", "w");
$csvString="";

$html = str_get_html(trim($table));
foreach($html->find('tr') as $element)
{

    $td = array();
    foreach( $element->find('th') as $row)
    {
        $row->plaintext="\"$row->plaintext\"";
        $td [] = $row->plaintext;
    }
    $td=array_filter($td);
    $csvString.=implode(",", $td);

    $td = array();
    foreach( $element->find('td') as $row)
    {
        $row->plaintext="\"$row->plaintext\"";
        $td [] = $row->plaintext;
    }
    $td=array_filter($td);
    $csvString.=implode(",", $td)."
";
}
echo $csvString;
fclose($fp);
exit;

}

Baba's answer contains extra space. So, I updated the code to this:

include "simple_html_dom.php";
$table = '<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>';

$html = str_get_html($table);



header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=sample.csv');

$fp = fopen("php://output", "w");

foreach($html->find('tr') as $element)
{
    $td = array();
foreach( $element->find('th') as $row)
{
    $td [] = $row->plaintext;
}

foreach( $element->find('td') as $row)
{
    $td [] = $row->plaintext;
}
fputcsv($fp, $td);
}


fclose($fp);

</div>



我之前从未真正做过这个,但我发现这个教程包含了源文件,而且它很简单,很容易 按照:</ p>

http://davidvielmetter.com/tricks/howto-convert-an-html-table-to-csv-using-php/ </ p>
</ div>

展开原文

原文

I've never actually had to do this before, but I found this tutorial which includes source files, plus it's short and very easy to follow:

http://davidvielmetter.com/tricks/howto-convert-an-html-table-to-csv-using-php/

I've adapted a simple class based on the code found on this thread that now handles colspan and rowspan. Not heavily tested and I'm sure it could be optimized.

Usage:

require_once('table2csv.php');

$table = '<table border="1">
    <tr>
    <th colspan=2>Header 1</th>
    </tr>
    <tr>
    <td>row 1, cell 1</td>
    <td>row 1, cell 2</td>
    </tr>
    <tr>
    <td>row 2, cell 1</td>
    <td>row 2, cell 2</td>
    </tr>
    <tr>
    <td rowspan=2>top left row</td>
    <td>top right row</td>
    </tr>
    <tr>
    <td>bottom right</td>
    </tr>
    </table>';

table2csv($table,"sample.csv",true);

table2csv.php

<?php

    //download @ http://simplehtmldom.sourceforge.net/
    require_once('simple_html_dom.php');
    $repeatContentIntoSpannedCells = false;


    //--------------------------------------------------------------------------------------------------------------------

    function table2csv($rawHTML,$filename,$repeatContent) {

        //get rid of sups - they mess up the wmus
        for ($i=1; $i <= 20; $i++) { 
            $rawHTML = str_replace("<sup>".$i."</sup>", "", $rawHTML);
        }

        global $repeatContentIntoSpannedCells;

        $html = str_get_html(trim($rawHTML));
        $repeatContentIntoSpannedCells = $repeatContent;

        //we need to pre-initialize the array based on the size of the table (how many rows vs how many columns)

        //counting rows is easy
        $rowCount = count($html->find('tr'));

        //column counting is a bit trickier, we have to iterate through the rows and basically pull out the max found
        $colCount = 0;
        foreach ($html->find('tr') as $element) {

            $tempColCount = 0;

            foreach ($element->find('th') as $cell) {
                $tempColCount++;
            }

            if ($tempColCount == 0) {
                foreach ($element->find('td') as $cell) {
                    $tempColCount++;
                }
            }

            if ($tempColCount > $colCount) $colCount = $tempColCount;
        }

        $mdTable = array();

        for ($i=0; $i < $rowCount; $i++) { 
            array_push($mdTable, array_fill(0, $colCount, NULL));
        }

        //////////done predefining array

        $rowPos = 0;
        $fp = fopen($filename, "w");

        foreach ($html->find('tr') as $element) {

            $colPos = 0;

            foreach ($element->find('th') as $cell) {
                if (strpos(trim($cell->class), 'actions') === false && strpos(trim($cell->class), 'checker') === false) {
                    parseCell($cell,$mdTable,$rowPos,$colPos);
                }
                $colPos++;
            }

            foreach ($element->find('td') as $cell) {
                if (strpos(trim($cell->class), 'actions') === false && strpos(trim($cell->class), 'checker') === false) {
                    parseCell($cell,$mdTable,$rowPos,$colPos);
                }
                $colPos++;
            }   

            $rowPos++;
        }


        foreach ($mdTable as $key => $row) {

            //clean the data
            array_walk($row, "cleanCell");
            fputcsv($fp, $row);
        }
    }


    function cleanCell(&$contents,$key) {

        $contents = trim($contents);

        //get rid of pesky &nbsp's (aka: non-breaking spaces)
        $contents = trim($contents,chr(0xC2).chr(0xA0));
        $contents = str_replace("&nbsp;", "", $contents);
    }


    function parseCell(&$cell,&$mdTable,&$rowPos,&$colPos) {

        global $repeatContentIntoSpannedCells;

        //if data has already been set into the cell, skip it
        while (isset($mdTable[$rowPos][$colPos])) {
            $colPos++;
        }

        $mdTable[$rowPos][$colPos] = $cell->plaintext;

        if (isset($cell->rowspan)) {

            for ($i=1; $i <= ($cell->rowspan)-1; $i++) {
                $mdTable[$rowPos+$i][$colPos] = ($repeatContentIntoSpannedCells ? $cell->plaintext : "");
            }
        }

        if (isset($cell->colspan)) {

            for ($i=1; $i <= ($cell->colspan)-1; $i++) {

                $colPos++;
                $mdTable[$rowPos][$colPos] = ($repeatContentIntoSpannedCells ? $cell->plaintext : "");
            }
        }
    }

?>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐