【问题标题】:PHP - HTML Table convert to CSV just works for ~ 700 rowsPHP - 将 HTML 表转换为 CSV 仅适用于 ~ 700 行
【发布时间】:2017-05-04 13:43:44
【问题描述】:

我正在尝试使用我的 PHP 脚本将 HTML 表格转换为 CSV。

我有很多 HTML 文件,每个文件只包含一个结构非常简单的表格

<table><th></th><tr><td></td></tr></table>

不同文件中的 HTML 表格有 300 到 2000 行。我的 PHP 脚本在一秒钟内将大约 800 行的所有文件转换为 CSV,一切正常。但是其他的(有 900 多行)不起作用。我总是得到一个空的 CSV 文件,里面只有“”(在 Excel 中打开)。

我在 MAMP 上本地运行脚本,PHP 错误日志显示该非工作文件:

PHP 致命错误:未捕获的错误:调用 /Applications/MAMP/htdocs/convertcsv.php:29 中布尔值的成员函数 find()

这是我在 convertcsv.php 脚本中完成整个转换的唯一功能:

function convertToCSV($input,$output) {

$newFileContent = "";

file_put_contents($output, $newFileContent);
echo "File created (" . $output . ")";

$table = file_get_contents($input);
$html = str_get_html($table);

//Generate the CSV file header
header("Content-type: application/vnd.ms-excel");
header("Content-Encoding: UTF-8");
header("Content-type: text/csv; charset=UTF-8");

$fp = fopen($output, "w");
fwrite($fp,"\xEF\xBB\xBF");

foreach($html->find('tr') as $element) {
    $td = [];
    $kinder = $element->children();
    foreach( $kinder as $kind) {
        $td[] = $kind->plaintext;
    }
    fputcsv($fp, $td, ';');
}

fclose($fp);

}

第 29 行是 foreach 循环。

也许您知道为什么该脚本可以完美地处理多达 800 行的表格,而不是更大的表格?

非常感谢大家

【问题讨论】:

    标签: php html csv mamp


    【解决方案1】:

    我发现了问题。我只需要增加包含的 simplehtmldom.php 中的 MAX_FILE_SIZE - Greetings

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      相关资源
      最近更新 更多